本文介绍了如何转换日期字符串,如“2011-01-12T14:17:55.043Z”长到1294841716?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要转换这个字符串格式的日期:
I need to convert a date in this string format:
2011-01-12T14:17:55.043Z
"2011-01-12T14:17:55.043Z"
到一个数字,如1294841716(这是1970年1月1日以来的秒数[毫秒])。有解决方法吗?
to a number like 1294841716 (which is the number of seconds [not milliseconds] since Jan. 1st, 1970). Is there an easy way to do this parsing?
更新:以下是我到目前为止的代码:
Update: Here is the code I've got so far:
NSString *dateString = @"2011-01-12T14:17:55.043Z";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:@"yyyy-MM-ddTHH:mm:ss.nnnZ"];
NSDate *parsed = [inFormat dateFromString:dateString];
long t = [parsed timeIntervalSinceReferenceDate];
但 t
每次回到0
推荐答案
使用获取一个 NSDate
然后使用 - (NSTimeInterval)timeIntervalSince1970
从1970年起获得秒数。
Use NSDateFormatter to get a NSDate
then use - (NSTimeInterval)timeIntervalSince1970
to get the seconds since 1970.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:@"2011-01-12T14:17:55.043Z"];
NSLog(@"Date: %@", date);
NSLog(@"1970: %f", [date timeIntervalSince1970]);
NSLog(@"sDate: %@", [formatter stringFromDate:date]);
[formatter release];
这篇关于如何转换日期字符串,如“2011-01-12T14:17:55.043Z”长到1294841716?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!