Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

ios - Add next week (7 days) from current var (Int) to 10 digit unix timestamp in swiftui

To allow users change their username one time per week i want to add limit. So i have loginUpdateDate var and it returns 10 digit timestamp (Int) and it's ok. How can i add 7 days to this variable? Not from the beginning of the week, namely from the moment of rewriting this variable. I'm trying lastLoginUpdate * 24 * 60 * 60 but seems it wrong (I'm studying). So i need to this Int variable (10 digit) add 7 days. And then from tis variable subtract one day every day (To show user how many days remaining to next login change availability)

question from:https://stackoverflow.com/questions/65897700/add-next-week-7-days-from-current-var-int-to-10-digit-unix-timestamp-in-swif

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Depending on the accuracy you want either add 7 * 24 * 60 to your timestamp as suggested in the comments is one way.

Note you could also create a date from the timestamp and use the appropriate date functions as described here: How do I add 1 day to an NSDate?

if your loginUpdateDate is a Swift Date, just use the code in the link. Otherwise use the timestamp (the 10 digit int) to create a date:

let lastLoginDate = Date(timeIntervalSince1970: lastLoginUpdate)
var dayComponent    = DateComponents()
dayComponent.day    = 1 // For removing one day (yesterday): -1
let theCalendar     = Calendar.current
let nextAllowedDate        = theCalendar.date(byAdding: dayComponent, to: lastLoginDate)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...