Get NSDate Object N seconds from current date

suggest change

The number of seconds from the current date and time for the new date. Use a negative value to specify a date before the current date.

Use NSDate(timeIntervalSinceNow:) in Swift 2, Date(timeIntervalSinceNow:) in Swift 3, or +dateWithTimeIntervalSinceNow: in Objective-C. There is no method named dateWithTimerIntervalSinceNow.

For a date one week after today and one week before today:

Swift

let totalSecondsInWeek: NSTimeInterval = 7 * 24 * 60 * 60
// Positive value: one week after today
let nextWeek = NSDate(timeIntervalSinceNow: totalSecondsInWeek)

// Negative value: one week before today
let lastWeek = NSDate(timeIntervalSinceNow: -totalSecondsInWeek)

Swift 3

let totalSecondsInWeek:TimeInterval = 7 * 24 * 60 * 60;

//Using positive value to add to the current date
let nextWeek = Date(timeIntervalSinceNow: totalSecondsInWeek)

// Negative value: one week before today
let lastWeek = Date(timeIntervalSinceNow: -totalSecondsInWeek)

Objective-C

NSTimeInterval totalSecondsInWeek = 7 * 24 * 60 * 60;
//Using negative value for previous date from today
NSDate *lastWeek = [NSDate dateWithTimeIntervalSinceNow:-totalSecondsInWeek];

//Using positive value for future date from today
NSDate *nextWeek = [NSDate dateWithTimeIntervalSinceNow:totalSecondsInWeek];

NSLog(@"Last Week: %@", lastWeek);
NSLog(@"Right Now: %@", [NSDate date]);
NSLog(@"Next Week: %@", nextWeek);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents