How to use NSDateTimeFormatter to convert this string to UnixTime?
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.
精彩评论