iosruby-on-railsdate

Rails date to NSDate


I am trying to convert date from rails api to NSDate object. The response date from api is 2014-11-05T16:29:09.614Z What does .614Z mean?


Solution

  • The int before the Z are the milliseconds, the Z stand for Zulu which is the time zone:

    From http://en.wikipedia.org/wiki/Coordinated_Universal_Time:

    The UTC time zone is sometimes denoted by the letter Z—a reference to the equivalent nautical time zone (GMT), which has been denoted by a Z since about 1950. The letter also refers to the "zone description" of zero hours, which has been used since 1920 (see time zone history). Since the NATO phonetic alphabet and amateur radio word for Z is "Zulu", UTC is sometimes known as Zulu time.

    Thus you time format is:

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    

    If you include the Z like in the example above the date will correctly converted to the devices current timezone.