Converting NSString to NSDate does not work

综合技术 2017-11-15

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!

责编内容by:Hello, buddy! (源链)。感谢您的支持!

您可能感兴趣的

NSLog prints a different string from the NSString ... This is something that I'm truly baffled by: when I initialize a string with for...
NSNumber转NSString丢精问题 问题引出 在开发中,涉及价格金额处理,后台会返回Number类型的数据,打印或者经过Json转Model后的NSString可能出现精度丢失的问题,如果涉及...
Running an NSObject to NSString I am working through the Stanford iPhone class and I can't figure out why I ...
IOS – NSString to uint iOS NSString Flatten Html while retaining paragraph tags I...
NSString for NSDate conversion using NSDateformatt... I have a string of date 2010-11-29T00:00:00+05:30 and I want to convert it into...