Вопрос

I have this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:@"yyyy-MM-DD"];
NSDate *offsetDay = [dateFormatter dateFromString:entry.date];

String entry.date is 2014-02-16 and resulting offsetDay is 2014-01-16. Why is this happening??

Thanks.

Это было полезно?

Решение

Give this a try:

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

If I remember correctly, DD is day of the year, not day of the month like you want. You can find a complete NSDateFormatter formatting table here

Другие советы

try this code edit.

 NSString *entry=@"2014-01-16";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *offsetDay = [dateFormatter dateFromString:entry];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];

NSLog(@"date--%@",[dateFormatter stringFromDate:offsetDay]);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top