开发者

How to use NSDateTimeFormatter to convert this string to UnixTime?

开发者 https://www.devze.com 2023-02-20 12:10 出处:网络
Here is the time string : 30 MAR 2011 10:14:28 And I would like to convert it to the time in millisecond since 1970. How can I do so? Thank开发者_运维技巧 you.The class is NSDateFormatter, not NSDa

Here is the time string :

30 MAR 2011 10:14:28

And I would like to convert it to the time in millisecond since 1970. How can I do so? Thank开发者_运维技巧 you.


The class is NSDateFormatter, not NSDateTimeFormatter. You would first initialize the formatter with the appropriate format, something like this:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];

Then use it to parse the date string:

NSDate *date = [formatter dateFromString:string];

Then you can easily get a double representing seconds since 1970 from the date:

NSTimeInterval seconds = [date timeIntervalSince1970];

And then multiply by 1000 to get milliseconds.

0

精彩评论

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