1

Today one of my client report a bug that one of the application functionality suddenly stop work. After I unit-test the functionality, I come to know that NSDateFormatter is converting a wrong date. May be you had also experienced the same. below is my code

 self.frmt2=[[NSDateFormatter alloc]init];
 [self.frmt2 setDateFormat:@"YYYY-MM-dd”];
 self.str_DateTogetData=[self.frmt2 stringFromDate:[NSDate date]];

If my system date is between 29-12-2014,30-12-2014,31-12-2014, then after converting date into string its shows 29-12-2015,30-12-2015,31-12-2015 but if my system date is not between these three dates then above code convert NSDate into string correctly.but if I am using below code then its converting NSDate into string correctly

 self.frmt2=[[NSDateFormatter alloc]init];
 [self.frmt2 setDateFormat:@"yyyy-MM-dd”];
 self.str_DateTogetData=[self.frmt2 stringFromDate:[NSDate date]];

My problem is solved but I am in seek of knowledge why its happen. Any one have answer of it.

Karamjeet Singh
  • 460
  • 1
  • 5
  • 16
  • b/t is between , I have update my question – Karamjeet Singh Dec 30 '14 at 10:04
  • 2
    Here is the difference. http://stackoverflow.com/questions/15133549/difference-between-yyyy-and-yyyy-in-nsdateformatter – Dheeraj Singh Dec 30 '14 at 10:05
  • Yep, you can also check which standard is used at different iOs versions [at the documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1). – A-Live Dec 30 '14 at 10:07
  • Now I know the difference b/t YYYY & yyyy. Thanks to all of you. – Karamjeet Singh Jan 07 '15 at 14:24

3 Answers3

4

yyyy specifies the calendar year, whereas YYYY specifies the year (of “Week of Year”).

From Date Formatter Documentation, check the last 2 points from Use Format Strings to Specify Custom Formats section

arthankamal
  • 6,341
  • 4
  • 36
  • 51
  • 1
    The good thing is that at the end of the year programmers notice the difference. I wonder how many YYYYs there are in apps everywhere that are wrong. – gnasher729 Dec 30 '14 at 10:09
  • this can be understood but if you can look into my question there is a 1 year of difference into dates. **If my system date is between 29-12-2014,30-12-2014,31-12-2014, then after converting date into string its shows 29-12-2015,30-12-2015,31-12-2015** this should not be happen. because I had tried to print 29-12-2013 & its print correctly. – Karamjeet Singh Dec 30 '14 at 10:12
  • @gnasher729 yeah :) :) :) :) – arthankamal Dec 30 '14 at 10:12
  • @karam you might need to research these year-weeks calendars if you want to use them. – A-Live Dec 30 '14 at 10:17
  • @karam: Of course it should happen, because you used a date format that _explicitely requires it to happen_. 29th Dec 2014 is in the first calendar week of 2015, not the last calendar week of 2014. – gnasher729 Dec 30 '14 at 10:30
  • @karam: Try Jan 1st 2014. Open a calendar app, and you will see why. – gnasher729 Dec 30 '14 at 10:32
1

-small mistake in your date formatter

It uses yyyy to specify the year component. 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

your date formatter should be in this formate [self.frmt2 setDateFormat:@"yyyy-MM-dd"]

0

Your "knowledge" that YYYY and yyyy are the same is totally wrong.

It has been explained several times in this thread that they are not the same, and now you have actually updated your question to say that they are the same. They are not.

Did you understand at all what YYYY is for? It is for situations where you number weeks. Week 1 of 2014, week 2 of 2014, and so on. The week from Monday, 29th Dec 2014 to Sunday, 4th March 2015 is one week, and because four of its days are in 2015, it's the first week of 2015.

gnasher729
  • 51,477
  • 5
  • 75
  • 98