I have a NSString which have the format of @"2013-01-09 06:10:10 +0000" (I am getting it from server and it is not the current time). I want to increment it by one second continuously. I can use a timer for doing it, but how to increment the time by one second?
Nowadays (2017) Apple recommends to use (NS)Calendar
for all kinds of date math
Objective-C
NSDate *now = [NSDate date];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *nowPlusOneSecond = [currentCalendar dateByAddingUnit:NSCalendarUnitSecond
value:1
toDate:now
options:NSCalendarMatchNextTime];
Swift 3
let now = Date()
let currentCalendar = Calendar.current
let nowPlusOneSecond = currentCalendar.date(byAdding: .second, value: 1, to: now)!
Try this,
NSDate *correctDate = [NSDate dateWithTimeInterval:1.0 sinceDate:yourDate];
You can get yourDate from string using NSDateFormatter
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With