Question

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 ?

Was it helpful?

Solution

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;

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top