RexOnRoids
RexOnRoids

Reputation: 14040

How do I get a timestamp from an NSString date?

I have an NSString date (@"Mon, 01 Feb 2010 20:25:37 +0000") that I want to change into a timestamp so that I can better calculate how much time has elapsed from the current time. How can I change the NSString date into a timestamp value?

Upvotes: 1

Views: 2344

Answers (2)

Giao
Giao

Reputation: 15146

Haven't fully tested this out but it should do the trick.

NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZ" allowNaturalLanguage:NO];
NSDate *date = [formatter dateWithString:@"Mon, 01 Feb 2010 20:25:37 +0000"];

Upvotes: 1

kennytm
kennytm

Reputation: 523214

Use +[NSDate dateWithString:] if the format is "2001-03-24 10:45:32 +0600".

If not, you need to construct an NSDateFormatter, then use -[NSDateFormatter dateFromString:].

Upvotes: 1

Related Questions