86

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 calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

But when I try to use

NSString *stringDate = @"Feb 28, 2013 05:30pm";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy hh:mma"];
NSDate *date=[dateFormatter dateFromString:stringDate];
NSLog(@"Date 1 : %@",date); //2013-02-28 12:00:00 +0000

NSString *stringDatee = @"Feb 28, 2013 05:30pm";
NSDateFormatter *dateFormatterr = [[NSDateFormatter alloc] init];
[dateFormatterr setDateFormat:@"MMM dd, YYYY hh:mma"];
NSDate *datee=[dateFormatterr dateFromString:stringDatee];
NSLog(@"Date 2 : %@",datee); //2013-01-05 12:00:00 +0000

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM dd, YYYY hh:mma"];
NSString *dateString = [dateFormat stringFromDate:datee];
NSLog(@"date 3 : %@", dateString); //Jan 05, 2013 05:30PM

As here, result to date and datee different, which I understood, but why result of date 2 and date 3 are different? As I am creating date from string and reversing same to string again, but output mismatches?

Has anybody knows reason about same?. Though it specifies week of year, still I should get result same.

Thanks..

EDIT :-

If I code

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM dd, YYYY hh:mma"];
NSString *dateString = [dateFormatterr stringFromDate:[NSDate date]];
NSLog(@"date: %@", dateString); //Feb 28, 2013 04:37PM

If results me proper result, but same which I pass as string to date I get 2013-01-05 12:00:00 +0000, check date 2 of NSLog, Strange result, why?

P.J
  • 6,547
  • 9
  • 44
  • 74
  • 1
    One should watch the WWDC 2011 Video called "Session 117 - Performing Calendar Calculations", 59:11, 142.2 MB (HD)" to get enlightened about virtual reality a là StarTrek? Nope. I mean calendar-based calculations- and what not. If u haven't seen this video please watch it, it's very interesting if u like stuff like that. –  Feb 28 '13 at 11:11
  • [ISO week date](http://en.wikipedia.org/wiki/ISO_week_date) – Parag Bafna Feb 28 '13 at 11:35
  • 2
    Mainly, YYYY works some of the time, while yyyy works all of the time. If you look at [the spec](http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns) then it's (sorta) clear why. – Hot Licks Apr 07 '14 at 17:23
  • And the difference between date2 and date3 has nothing to do with the year format and is purely due to the fact that the NSDate `description` method always displays that way (and in UTC) -- there is no "format" (or timezone) in an NSDate. – Hot Licks Apr 07 '14 at 17:28

4 Answers4

79

Also when using a date format string using the correct format is important.

@"YYYY" is week-based calendar year.

@"yyyy" is ordinary calendar year.

You can go through the whole blog, its a good to give it a look

enter image description here

https://web.archive.org/web/20150423093107/http://realmacsoftware.com/blog/working-with-date-and-time

http://realmacsoftware.com/blog/working-with-date-and-time (dead link)

André C. Andersen
  • 8,955
  • 3
  • 53
  • 79
X-Factor
  • 2,067
  • 14
  • 18
  • Thanks for your answer, have you checked my NSLog of date 2 and date 3? Why are they different? I am passing same format, and same thing.. – P.J Feb 28 '13 at 10:54
  • 1
    I hope its because you are converting a date object into a string, thats why it is giving 2 different results. – X-Factor Feb 28 '13 at 10:59
  • whenever you convert a date into a string it shows you the same result you are seen check the blog http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html – X-Factor Feb 28 '13 at 11:00
  • I am doing string->date and then date->string with same formatter, so ideally is should return me same date again which I passed earlier, but it doesn't.. Why? – P.J Feb 28 '13 at 11:00
  • No thats the case, whenever you convert a date object into string, it gives you the result like (Jan 05, 2013 05:30PM - full object) i have given you the link go through it. – X-Factor Feb 28 '13 at 11:03
  • No, dont agree with you, reason is that YYYY shows me week based calendar, that is right, but when i convert this week based calendar, I should get correct output? – P.J Feb 28 '13 at 11:03
  • Sorry bro, then i think i dont have any logical reasoning on this issue except that its the tostring method of NSDate, which converts date into that format :) – X-Factor Feb 28 '13 at 11:05
  • 4
    The link is down. – Clashsoft Jan 18 '19 at 22:21
  • Content in link is now recovered. – André C. Andersen Feb 08 '20 at 21:24
34

A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

from Apple Docs

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Gardner Bickford
  • 1,934
  • 1
  • 17
  • 25
  • Sorry, my bad. There are 52 weeks in a year. This value should be return which week of the year the date is. This would be good for calculating finance items or recurring calendar events. – Gardner Bickford Feb 28 '13 at 11:02
  • 2
    I agree that this identifier is confusing and was not well selected. There are standards reasons why it must exist: https://en.wikipedia.org/wiki/ISO_week_date – Gardner Bickford Feb 28 '13 at 11:05
  • datee is an NSLog object. It will return a string representation of itself when concatenated with a string. This means that you are relying on the default format of NSDate when passing datee to NSLog(). If you create NSDate objects with dateWithString you may see the difference. – Gardner Bickford Feb 28 '13 at 11:39
4
dd/MMM/YYYY - e.g.:1 01/Jan/2000; answer : 19/dec/1999
              (see weekly calendar December month last Monday
              suppose leaf year + 1 day)
dd/MMM/yyyy - eg: ordinary - no problem.
sKhan
  • 9,694
  • 16
  • 55
  • 53
gowthaman-mallow
  • 692
  • 1
  • 6
  • 10
1

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

CoderPug
  • 908
  • 9
  • 19