开发者

NSDateFormatter: How to read dates in March

The NSDateFormatter can be used to construct NSDate instances from date strings. This works except with date strings for March, where the MMM month format is used.

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"dd-MMM-yyyy"];
NSDate *aDate = [fmt dateFromString:dateString];
[fmt release];

For example, the date开发者_如何转开发String value:

  • @"29-Jan-2011" works
  • @"04-Feb-2011" works
  • @"20-Mar-2011" does not work! Resulting NSDate instance aDate is nil

Is this a bug in iOS SDK 4.3 or does the formatter expect a different abbreviation for March?


I cannot reproduce this:

NSString *dateString = @"20-Mar-2011";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"dd-MMM-yyyy"];
NSDate *aDate = [fmt dateFromString:dateString];
[fmt release];
NSLog(@"date=%@", aDate);

Output:

2011-03-17 22:04:39.798 Untitled[18025:207] date=2011-03-20 04:00:00 +0000

Is it possible you have your locale set to something other than English?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜