Wednesday, 13 July 2016

Update hours/minutes/seconds in NSDate | Get current date from NSDate but set custom time

 
// 24 hour date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

// Now date + 12 am time fixed (00:00:00)
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDate *nowDate = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:[NSDate date] options:0];
NSString *str = [dateFormatter stringFromDate:nowDate];
NSLog (@"String is: %@", str);

No comments:

Post a Comment