Event errors in yyyy and YYYY in NSDateFormatter--climbing pits

Source: Internet
Author: User

Yesterday was the weekend, but the company suddenly told me the online version, unable to request data. I tried it, and it was! The token overtime! of the pop-up window report This is odd, guess is the time is wrong, printed the event, found unexpectedly is December 27, 2016. I don't believe it, how can this good time be wrong, and then

1  NSDate *date = [[NSDate alloc]init]; 2     NSLog (@ "%@", date);

The output is right, December 27, 2015. That's NSDateFormatter's fault, and he tried it again.

1NSDate *date =[[NSDate alloc]init];2NSLog (@"%@", date);3NSDateFormatter *formatter =[[NSDateFormatter alloc] init];4[Formatter Setdateformat:@"YYYYMMDD"];5NSString *datestr =[Formatter stringfromdate:date];6NSLog (@"%@", DATESTR);

Oh da!! The result is December 27, 2016, I am really drunk, this can be wrong. Google on the search, and really someone has encountered, the original is the event format [formatter Setdateformat:@ "YYYYMMdd"]; with YYYY, And the Big Y represents the weekly count of December 27 is exactly the last weekend of 2015, and according to the United States, the weekend is the first day of the week, so press @ "YYYYMMdd"Formatter after it became 16 years.

Change the event grid to [formatter Setdateformat:@ "yyyyMMdd"];

everything OK;.

Everywhere is the pit, or should pay attention to code specification, careful!

Event errors in yyyy and YYYY in NSDateFormatter--climbing pits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.