开发者

why does this code display the date being SAT where with NSDateComponent setWeekday I used 1 for SUN?

Why does this code display the date being SAT where with NSDateComponent & setWeekday I used 1 for SUN?

// Set Day
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setWeekday:1];
[comps setHour:13];
[comps setMinute:30];

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *tempDate = [gregorian dateFromComponents:comps];

NSDateFormatter *format = [[[NSDateFormatter alloc] init] autorelease];
[format setDateFormat:@"EEE HH:mm"];    
NSString *s = [format stringFromDate:tempDate];
NSLog(@"Finishing : %@", s);

// Gave => 2011-04-05 09:50:28.467 test1[39840:2开发者_JAVA技巧07] Finishing : Sat 13:30


Converting an NSDateComponents with only the weekday, hour, and minute set apparently gives you a date in the year 1. As this is long before the Julian/Gregorian calendar transition, you will end up with odd results in some places where some methods interpret the date in the proleptic Gregorian calendar while others interpret it in the Julian calendar.

When I try your code with a more detailed date format, I get Sat 0001-01-01 13:30:00 -045602. Try it after using setYear: on the components and you'll get sensible results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜