ios - Specify the time offset with an NSDateFormatter (-0500) -
i have date strings similar these:
2013-01-25 00:00:00 -0500 2013-01-22 00:00:00 -0700 2013-01-26 00:00:00 -0200
i want use nsdateformatter
create nsdate
using these kinds of strings. know how use formatter first part of date (2013-01-25 00:00:00
), don't know how specify offset part (-0500
, -0200
, etc). here's code start of date string:
nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"yyyy'-'mm'-'dd hh':'mm':'ss"]; nsstring *datestring = @"2013-01-25 00:00:00"; // <-------- truncated ---- nsdate *date = [dateformatter datefromstring:datestring]; nslog(@"date: %@", date);
how can working -0500
part? tried sssz
, didn't work (gave null
date).
try this:
nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"yyyy'-'mm'-'dd hh':'mm':'ss zzz"]; nsstring *datestring = @"2013-01-25 00:00:00 -0500"; nsdate *date = [dateformatter datefromstring:datestring]; nslog(@"date: %@", date);
i get:
date: 2013-01-25 05:00:00 +0000
as side note, here standard date formats: http://unicode.org/reports/tr35/tr35-4.html#date_format_patterns
Comments
Post a Comment