将 NSString(2011-04-18 18:57:47.453000) 转换为 NSDate
嘿,伙计们,我搜索了很多,但无法让我的代码工作。
我得到一个字符串形式的日期,格式如下: 2011-04-18 18:57:47.453000
我想将其转换为 NSDate 以计算经过的时间,但我似乎无法正确转换。这就是我正在做的:
NSString *dateString = [entityData objectForKey:@"creation_time"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.fff"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
当我打印 dateString 时,我得到:2011-04-18 18:57:47.453000 但打印 dateFromString 给出: (null)
谁能指出我正确的方向?
Hey, guys i searched a lot but cant get my code working.
I am getting a date in form of a string with format like : 2011-04-18 18:57:47.453000
I want to convert this to NSDate to calculate time elapsed, but I cant seem to get the conversion right. here is what i am doing:
NSString *dateString = [entityData objectForKey:@"creation_time"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.fff"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
when i print dateString i get : 2011-04-18 18:57:47.453000
but printing dateFromString gives: (null)
Can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能不久前就解决了这个问题...但是:
可以在位于此处的“日期字段符号表”中找到有关转换日期组件的一些有用信息:
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
You probably solved this awhile ago... but:
Some good information on converting date components can be found in the "Date Field Symbol Table" located here:
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns