开发者

Issues with String to NSDate

开发者 https://www.devze.com 2023-04-09 11:02 出处:网络
My dateFromString is not working and i\'m not sure why NSString *purchase = @\"2011-09-30 17:47:57\"; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

My dateFromString is not working and i'm not sure why

NSString *purchase = @"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];

date is 'invalid CFStringRef'

开发者_JAVA百科

See anything i might be overlooking?


Try changing the hours in the formatter to capitals, i.e. yyyy-MM-dd HH:mm:ss

I'm not sure if that will solve your error, but that is the correct way to parse a 24 hour time


in the format
"hh" means Hour [1-12].
"HH" means Hour [0-23].

See UTS Date Field Symbol Table for the date format specifiers.


Try this:

NSString *purchase = @"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];

Date Formatters Guide - HH is used for 24 hour times, hh for 12 hour.


If you mean that "date" is not accepted as an NSString by another function, that's because it's not a string, it's an NSDate.

(Where, precisely, are you getting the error message, and what is the full text?)

0

精彩评论

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