xonegirlz
xonegirlz

Reputation: 8977

NSDate and time interval issues

I am trying to convert a NSString into a double using the following:

double lastSynced;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
               [dateFormatter setDateFormat:@"YYYY-MM-DD HH:MM:SS"];
               NSDate *dateFromString = [[NSDate alloc] init];
               dateFromString = [dateFormatter dateFromString:@"2012-03-28 18:41:25"];
               lastSynced = [dateFromString timeIntervalSince1970];

However when printing lastSynced it always gives me 0. Why is this?

Upvotes: 0

Views: 458

Answers (3)

Alexandru Lucian Susma
Alexandru Lucian Susma

Reputation: 199

Your formatting strings are not correct. You should use :

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

Good luck !

Upvotes: 0

Zalykr
Zalykr

Reputation: 1524

You have to do:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

instead of:

[dateFormatter setDateFormat:@"YYYY-MM-DD HH:MM:SS"];

Good luck!

Upvotes: 1

yuji
yuji

Reputation: 16725

Your date format is wrong. Should be:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

Upvotes: 4

Related Questions