开发者

Passing a formatted NSDate

开发者 https://www.devze.com 2023-04-11 22:43 出处:网络
开发者_运维百科Formatted dates that I am passing to an instance method are not saving/printing the correct date. Can someone please help?

开发者_运维百科Formatted dates that I am passing to an instance method are not saving/printing the correct date. Can someone please help?

Calling method code snippets:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd-mm-yyyy"];

[requests addObject:[[Request alloc] initWithDueDate:(NSDate *) [dateFormatter dateFromString:@"13-09-2011"]]];

Receiving method code snippets:

dueDate = dd;

NSLog(@"Due Date: %@", dueDate);

Console output:

Due Date: 2011-01-12 13:09:00 +0000


You have to check your formatting string. It follows these official formats.

[dateFormatter setDateFormat:@"dd-MM-yyyy"];

Small m is for minute, cap M for the month.


Next works perfect for me:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];

NSDate *date = [dateFormatter dateFromString:@"13-09-2011"];
NSLog(@"%@", date);


http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

Make sure you check your formatted NSDates to this. They are case-sentitive so make sure you use them correctly

0

精彩评论

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