-2

want to understand the Difference between the Difference between dateFromString and stringFromDate for this pattern "YYYY".

Because I have written the below logics for dateFromString and stringFromDate but result is different like Mon Dec 30 09:05:00 2019 and 2020/12/30 09:05

// convert string to date
     NSString *dateStr = @"2019-12-30 09:05";
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm"];
        //NSDate *date = [dateFormat dateFromString:dateStr];
        NSDate *dateValue = [dateFormat dateFromString:dateStr];
        NSLog(@"string to date == %@",dateValue);



        // convert date to string

        NSDateFormatter *df1 = [[NSDateFormatter alloc] init];

        NSLocale *locale = [NSLocale currentLocale];
        [df1 setLocale:locale];

        [df1 setDateFormat:@"YYYY/MM/dd HH:mm"];
        NSString *datestr = [df1 stringFromDate:dateValue];
        NSLog(@"date to string == %@", datestr);

so out put is below

 string to date == Mon Dec 30 09:05:00 2019
 date to string == 2020/12/30 09:05

Both or same pattern YYYY It is working as a week based calander. but why year value 2019 and 2020 is differing when using following methods dateFromString and stringFromDate?

iSara
  • 919
  • 3
  • 10
  • 24
  • Note that "yyyy" is different of "YYYY", usually you want "yyyy". Read the doc: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns – Larme Jan 06 '20 at 10:08
  • Use "yyyy" instead of "YYYY". – phani Jan 06 '20 at 10:08
  • 1
    Does this answer your question? [Difference between 'YYYY' and 'yyyy' in NSDateFormatter](https://stackoverflow.com/questions/15133549/difference-between-yyyy-and-yyyy-in-nsdateformatter) – vadian Jan 06 '20 at 11:19
  • Yes 'YYYY' is referring week based calendar, But same time I am getting two different of result in year 2019 and 2020 when using methods dateFromString and stringFromDate in same pattern ''YYYY'. – iSara Jan 06 '20 at 11:37
  • Does this happen when you use a date not from last week December/first week January? – Carla Camargo Jan 06 '20 at 12:52
  • @CarlaCamargo - No only the last week of December. But same formatter only different methods. want to understand the difference. – iSara Jan 06 '20 at 13:28
  • 1
    "string to date == Mon Dec 30 09:05:00 2019", because in "reality" it's in 2019. Since we commonly don't use week based calendar, but year based one, and XCode print it as such. That's a NSDate object that you printed, not a NSString. It's the same point converted in timeIntervalSince1970 if you want. Juste the description (print) is different – Larme Jan 06 '20 at 13:29

2 Answers2

0

When you use yyyy, you will get the calendar numeric year. The YYYY you will get the week of year, that can do this boundary rollover that you are experiencing. Read the docs at: http://www.unicode.org/reports/tr35/tr35-dates.html#dfst-year

Always when I have problems with date formatters I usually go to this website https://nsdateformatter.com/.

Carla Camargo
  • 574
  • 5
  • 14
  • Agree that 'YYYY' week of year calendar. But same time I am getting two different of result of year 2019 and 2020 when using methods dateFromString and stringFromDate in same pattern ''YYYY'?. – iSara Jan 06 '20 at 11:45
  • You shouldn't be using two different NSDateFormatter. Use only one and change the format only (both locale should be the same) – Carla Camargo Jan 06 '20 at 12:48
  • Thanks for your reply. even using same NSDateFormatter also the year is getting different 2019 and 2020 while using dateFromString and stringFromDate. so want to know the difference – iSara Jan 06 '20 at 13:25
  • 1
    Sorry, I don't know the deeper why of your question. They state in the documentation that it could be a possibility (because we have 52 weeks in the calendar, but in the majority of the time, the 1st day of the other year isn't the start of a new week, that's my guess why this is happening) – Carla Camargo Jan 06 '20 at 13:54
  • 1
    @iSara You printed a `NSDate` object. Apple decide when you print it (with `%@` format which is in fact calling `description` method, to show it as such with a "yyyy" format internally because it's the most common one. Previously, it didn't print the day, it was more like a ISO8601 look alike. It represent the same point in the time. – Larme Jan 06 '20 at 16:23
0

The week number of 2019-12-30 is week 1 of 2020. The year of week 1 of 2020 is 2020. Try format yyyy/MM/dd HH:mm YYYY w (week of year) the result is 2019/12/30 09:05 2020 1.

Willeke
  • 14,578
  • 4
  • 19
  • 47