0

I am geting some weird result from NSDateComponents and NSDateFormatter.

I have the following method to set the hour to 10am for the given date

+ (NSDate *)getTenOClockOnDate:(NSDate*)inputDate { NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *components = [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit ) fromDate:inputDate]; [components setHour:10]; [components setMinute:0]; [components setSecond:0]; // construct new date and return NSDate *newDate = [cal dateFromComponents:components]; NSLog(@"input date: %@ -- new date: %@", inputDate, newDate); return newDate; } 

While I make the following call:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; [MyUtils getTenOClockOnDate:[dateFormatter dateFromString:@"2010-12-31"]]; [MyUtils getTenOClockOnDate:[dateFormatter dateFromString:@"2011-01-01"]]; [MyUtils getTenOClockOnDate:[dateFormatter dateFromString:@"2011-01-02"]]; 

only the the second line, I get the unexpected result:

input date: 2010-12-31 05:00:00 GMT -- new date: 2010-12-31 15:00:00 GMT input date: 2011-01-01 05:00:00 GMT -- new date: 2011-12-31 15:00:00 GMT input date: 2011-01-02 05:00:00 GMT -- new date: 2011-01-02 15:00:00 GMT 

The problem is on the second record, 2011-01-01, it turns into 2011-12-31 just by setting the hour to 10.

3 Answers 3

1

Println(date) in Swift is incorrect, use nsdateformatter with stringfromdate

I guess this apply for nslog too, in Objective-C

Sign up to request clarification or add additional context in comments.

Comments

0

It does indeed look rather odd this... Whilst your code looks like it should be fine my hunch is it's going to be when you're initializing the NSDateComponents object you're not enabling the NSHourCalendarUnit and NSMinuteCalendarUnit.

Whilst you'd think this wouldn't be necessary I think I had a similar issue and found these needed to be on to modify the date's time correctly and get the result you're after.

If that doesn't work, perhaps trying to set the calendar's timezone may be necessary as they're the only differences to what I do and does seem to work a charm.

1 Comment

Ahhh, well spotted! I overlooked that one too.
0

This will give right NSDate value from NSDateComponents,

NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"yyyy-MM-dd"]; NSDate* date = [df dateFromString:@"2011-01-01"]; NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date]; NSInteger day = [components day]; NSInteger month = [components month]; NSInteger year = [components year]; [df release]; NSLog(@"Day : %d, Month : %d, Year : %d",day,month,year); 

It will may help you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.