Question

I have an NSDate which I want zero-out hour, minutes and seconds. The result I want is: 2014-02-19 00:00:00 +0000. I have tried the following:

NSUInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* components = [[NSCalendar currentCalendar] components:flags fromDate:self.birthDatePicker.date];
NSDate* birthDate = [[NSCalendar currentCalendar] dateFromComponents:components];

For some reason I get this: 2014-02-19 23:00:00 +0000. Hour isn't zeroed out. I have also tried to set [components setHour:0]. But I get the same result 23 for hours. Any ideas on what I'm doing wrong?

Was it helpful?

Solution 3

Always helpful:

WWDC Videos

2011 Session 117 - Performing Calendar Calculations

2013 Session 227 - Solutions to Common Date and Time Challenges


This contains the information you probably want:

Session 227 @ 13m25s, "Common Operations" / "Calculate Midnight".

OTHER TIPS

Consider this category on NSDate:

@interface NSDate (Utilities)
- (NSDate *) dateAtStartOfDay;
@end

@implementation
- (NSDate *) dateAtStartOfDay
{

   NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:self];
   [components setHour:0];
   [components setMinute:0];
   [components setSecond:0];
   return [[NSCalendar currentCalendar] dateFromComponents:components];
}
@end

Code taken from Erika Sadun NSDate Extension

Take a look at this library: https://github.com/mysterioustrousers/MTDates

I had the similar problem I've decided to use it. It has a lot of date related functions. The one you need is:

- (NSDate *)mt_startOfCurrentDay;

About time zones. NSDate has independent from time zone format. Time zone you need you can specify in NSDateFormatter instance.

Update Full code example

NSDate *date = [NSDate date];
NSLog(@"%@", date); // 2014-02-20 11:11:40 +0000
[NSDate mt_setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSLog(@"%@", [[date mt_startOfCurrentDay] mt_stringFromDateWithISODateTime]); // 2014-02-20 00:00:00 +0000

For other date format, setup NSDateFormatter with time zone and your custom date format

One more solution This one without library:

NSDate *date = [NSDate date];
NSLog(@"%@", date); // 2014-02-20 11:36:20 +0000
NSUInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:currentCalendar.calendarIdentifier];
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDateComponents* components = [calendar components:flags fromDate:date];
date = [calendar dateFromComponents:components];
NSLog(@"%@", date); // 2014-02-20 00:00:00 +0000

P.S. Btw I have GMT+3 time zone

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top