I have a date like :

2014-01-31 17:40:56

How Can I convert it in Timestamp with millisecond?

The result will be 1391170256309 , which is calculated by PHP.

How can I get 2014-01-31 17:40:56 To 1391170256309 in Objective C ?

有帮助吗?

解决方案

Does this work for what you're trying to do?

NSString * dateString = @"2014-01-31 17:40:56";
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *date = [df dateFromString:dateString];

NSTimeInterval since1970 = [date timeIntervalSince1970]; // January 1st 1970

double result = since1970 * 1000;

其他提示

NSDate has a factory method for this specific scenario

- (NSTimeInterval)timeIntervalSince1970

The result that is being calculated in called "epoch time".

Here is sample code I use for NSDate to --> milliseconds :

NSTimeInterval seconds = [NSDate timeIntervalSinceReferenceDate];
double milliseconds = seconds*1000;

Note : reference date is 1 January 2001, GMT hence multiply it with 1000

Reference - Apple Official Docs

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top