How would I get the current time in ISO 8601 format? It should look something like 2011-11-16T22:06Z
Asked
Active
Viewed 3,469 times
4
-
1Can't you use a simple NSDateFormatter call for that -- "yyyy-MM-dd'T'HH:mmZ" or some such? (Don't forget to set Locale to avoid the AM/PM mess.) – Hot Licks Nov 17 '11 at 03:19
-
@Daniel R Hicks I did just that and it worked! do you want to convert it to an answer so I can accept it? – Suchi Nov 18 '11 at 22:24
3 Answers
5
A pure C solution, using only standard C features:
#include <stdio.h>
#include <time.h>
int main(void) {
time_t now = time(NULL);
struct tm *now_tm = gmtime(&now);
char iso_8601[] = "YYYY-MM-DDTHH:MMZ";
/* init just to get the right length */
strftime(iso_8601, sizeof iso_8601, "%FT%RZ", now_tm);
puts(iso_8601);
return 0;
}

Keith Thompson
- 254,901
- 44
- 429
- 631
4
use a simple NSDateFormatter call for that -- "yyyy-MM-dd'T'HH:mmZ" or some such. (Don't forget to set Locale to avoid the AM/PM mess.)

Hot Licks
- 47,103
- 17
- 93
- 151
-
2
-
In my case yyyy-MM-dd'T'HH:mm'Z' doesn't work for ISO 8601 if date is 2013-11-29T12:42+06.00. yyyy'-'MM'-'dd'T'HH':'mmZ works fine – user1264176 Nov 29 '13 at 12:44
-
@user1264176 - I've never seen a timezone with a dot in it like that. For a timezone with a colon you use ZZZZZ. For no colon Z works fine. – Hot Licks Nov 29 '13 at 13:12
-
What I meant is that I had to remove ' (apostrophe) which wraps Z. – user1264176 Nov 29 '13 at 13:30
-
And yeah, sorry, the timezone is actually with colon. I've made a typo. – user1264176 Nov 29 '13 at 14:00
-
@user1264176 - The format listed by the OP included a literal "Z" (for Zulu/GMT), so Bot's comment was correct. So far as I know the formatter will not read/write that symbol, so it must be literally coded (with Z in single quotes), and the timezone of the formatter must be explicitly set to GMT. – Hot Licks Nov 29 '13 at 16:13
-
@HotLicks I have two servers lets say dev and prod and iOS app that runs on iOS7. Dev gives me 2013-11-29T12:42Z and prod 2013-11-29T12:42+06:00. In case yyyy-MM-dd'T'HH:mm'Z' format I can work fine with dev server but if I receive prod's date NSDateFormatter returns me nil. In case yyyy-MM-dd'T'HH:mmZ format my app works fine with both dev and prod. – user1264176 Dec 02 '13 at 09:01
-
@user1264176 - Yeah, I've never tried reading a "Z" with a "Z" specifier, so I can't say whether it works or not. (The documentation is not particularly helpful on this point.) In some cases it is necessary to examine the received date's structure first and either select from several or massage the format into something the formatter can handle. – Hot Licks Dec 02 '13 at 11:55
3
I found your answer by looking at this potentially duplicate question and one of the answers says to use a open source solution called Peter Hosey's ISO8601DateFormatter.
Which you can download from here. Bonus, it was updated only a few days ago (5-November-2011).
And, to get the current date & time... you'd do:
ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
[formatter release];
formatter = nil;

Community
- 1
- 1

Michael Dautermann
- 88,797
- 17
- 166
- 215
-
1
-
1This is the right answer, a single file is no library! And its an amazingly good piece of code too... – Brett Jun 08 '13 at 02:29