Difference between 'YYYY' and 'yyyy' in NSDateFormatter

后端 未结 4 524
北恋
北恋 2020-11-22 15:43

What is exact difference between \'YYYY\' and \'yyyy\'. I read in this link, it states that

A common mistake is to use YYYY. yyyy specifies the calen

4条回答
  •  没有蜡笔的小新
    2020-11-22 16:10

    All answers differentiating yyyy and YYYY are right answers for another question. The question itself refers to another thing.

    Why does these two values are different? (extracted from question)

    NSLog(@"Date 2 : %@",datee); //2013-01-05 12:00:00 +0000
    NSLog(@"Date 3 : %@", dateString); //Jan 05, 2013 05:30PM
    

    The answer here @P.J is that they are not really different in value. When you log an NSDate (which is Date 2) you are getting the full description of your object which happens to be on UTC Timezone. This logic does not happen when logging Date 3 because it was already converted to a String and applied your Timezone.

    For printing Date 3 the 'same way' as you are getting Date 2. You should specify UTC TimeZone for Date 3. Something like this :

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMM dd, YYYY hh:mma"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    NSString *dateString = [dateFormat stringFromDate:datee];
    NSLog(@"date 3 : %@", dateString); 
    

    Hope this helps.

    tl;dr the Timezone

提交回复
热议问题