read

When it comes to dealing with date, it is never simple.

The Apple programming guide has a chapter on NSDateFormatter, but it is not clear enough.

The Format

Refer to unicode.org for the date format pattern. It is so important a document that you should bookmark it!

Escaping

To escape in the pattern string, you can enclose with single quote.

For example, to escape the character “T”, you use 'T'HH:mm:ss. The pattern produces eg. T09:30:00

‘Z’ is for Zulu Time

Another common format is yyyy-MM-dd'T'HH:mm:ss'Z'

Eg. 2015-04-18T01:32:40.123Z

The Z at the end refers to Zulu time, which is also GMT and UTC. It is the RFC 3339 format.

Zulu sounds like zero - the zero hours.

NSDate

NSDate don’t have a time zone.

It is basically an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

NSCalendar

The most widely used calendar is gregorian, but there are others.

NSTimeZone

Print out all the supported time zone with:

NSLog(@"%@", [NSTimeZone knownTimeZoneNames]);

If you know a name, you can init a timezone easily:

[NSTimeZone timeZoneWithName:@"Asia/Singapore"];

You can also refer to wiki for a list of the timezone names.

3rd Party Libraries

In JS world, moment.js is a very popular library to deal with date, time and relative moments.

YLMoment is a smilar one in Objective-C.


Image

@samwize

¯\_(ツ)_/¯

Back to Home