Date format, do you use the right one?

Source: Internet
Author: User

In IOS development, I ran into strange problems.

[dateformatterstringfromdate:adate];

Dateformatter is a NSDateFormatter object.

Adate is a NSDate object with a value of 2015-12-30 00:00.

This code is to convert this nsdate into NSString.

The result I got was amazing: "2016-12-30 00:00"!!!

Again, this time the value of the NSDate object is changed to 2017-12-30 00:00.

The result is: "2017-12-30 00:00".

I'm crazy to see this, isn't Apple's code so obviously wrong?

After a period of testing and online search, finally found the reason.

There was an error in the formatting of the NSDateFormatter object.

I am using "yyyy-mm-dd". Change to "Yyyy-mm-dd". Problem Solving!!!

So what's the difference between YYYY and YYYY? YYYY is the standard year, while YYYY is the year of the ISO Year-week calendar.

Reference Links:

Http://stackoverflow.com/questions/15133549/difference-between-yyyy-and-yyyy-in-nsdateformatter

Http://stackoverflow.com/questions/12423179/nsdateformatter-show-wrong-year

Date format, do you use the right one?

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.