Converting NSString to NSDate does not work

I’ve read the DateFormatting guide and I’m still not able to get why my date conversion is not working as per requirement. For example:

NSString *str1 = @"12/20/2013 12:39:55 PM";
NSDateFormatter *dateFo = [[NSDateFormatter alloc] init];

[dateFo setDateFormat:@"MM/dd/YYYY hh:mm:ss a"];
NSDate *serverDate1 = [NSDate date];
serverDate1 = [dateFo dateFromString:str1];

In return I’m getting date as ” 2013-01-05 07:09:55 +0000
“. I have tried a lot why NSDate is not returning correct date.

Any help would be appreciated.

Instead of YYYY
use yyyy
in your dateFormat:
.

NSString *str1 = @"12/20/2013 12:39:55 PM";
NSDateFormatter *dateFo = [[NSDateFormatter alloc] init];

[dateFo setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *serverDate1 = [NSDate date];
serverDate1 = [dateFo dateFromString:str1];
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Converting NSString to NSDate does not work

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录