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

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

+4
source share
3 answers

use a simple NSDateFormatter call to do this - "yyyy-MM-dd'T'HH: mmZ" or some of them. (Remember to install Locale to avoid AM / PM mess.)

+4
source

I found your answer looking at this potentially duplicate question , and one of the answers says to use an open source solution called Peter Hosey ISO8601DateFormatter.

What you can download here . Bonus, it was updated just a few days ago (November 5, 2011).

And to get the current date and time ... you would do:

ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init]; NSString *dateString = [formatter stringFromDate:[NSDate date]]; [formatter release]; formatter = nil; 
+3
source

A pure C solution using only standard C functions:

 #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; } 
+3
source

Source: https://habr.com/ru/post/1381612/


All Articles