Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - how to get current time in ISO 8601 format?

How would I get the current time in ISO 8601 format? It should look something like 2011-11-16T22:06Z

like image 654
Suchi Avatar asked Dec 17 '22 07:12

Suchi


2 Answers

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;
}
like image 177
Keith Thompson Avatar answered Dec 28 '22 01:12

Keith Thompson


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.)

like image 42
Hot Licks Avatar answered Dec 28 '22 01:12

Hot Licks