0

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)

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Don't multiply your last login date by your time offset. Add it instead. `lastLoginUpdate += 7 * 24 * 60 * 60`. From there, you could just get the current date and compare the two rather than trying to subtract one every day. – Liftoff Jan 26 '21 at 08:08
  • @David maybe you abble to help me please i'm trying to add check for current time in var with next week but it's not working, something like this let currentDate = Int(NSDate().timeIntervalSince1970) if loginUpdateDate < currentDate += 7 * 24 * 60 * 60, i have "The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions" – Ермолай Абузув Jan 27 '21 at 09:28

1 Answers1

0

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)
Volker
  • 4,640
  • 1
  • 23
  • 31
  • 1
    Not every day has 24 hours. Therefore 7 * 24 * 60 * 60 might give you unexpected results. You should always use the calendar method with the desired timezone. – Leo Dabus Jan 26 '21 at 14:26
  • Please post a new question and not to the comment. And also take a look at NSDate / Date methods for getting time differences. – Volker Jan 27 '21 at 10:36