Swift returns the day of the week based on the date string

Source: Internet
Author: User
Tags dateformat time zones

A recent iOS project that requires a date based on a date represents the day of the week and the date is obtained as a string, so the method can be simply described as follows:

/** returns a date based on a date format string * Parameters: DateTime, String type, date format string, formatted as "Yyyy-mm-dd HH:mm:ss" * return value: Date represents day of the week, int type, Monday to Sunday respectively: 1~7*/ Func getweekday (datetime:string)->int

Simple Baidu a bit, and also roughly browse the official API document, learned that NSDate has a timeIntervalSince1970 to calculate the fixed time difference, so formed the first version (the problematic version):

Func getweekday (datetime:string)->int{let    datefmt = NSDateFormatter ()    Datefmt.dateformat = "Yyyy-mm-dd HH : Mm:ss "let    date = datefmt.datefromstring (dateTime) let    interval = Int (date!. timeIntervalSince1970) let days     = Int (interval/86400)//24*60*60 to    weekday = ((days + 4)%7+7)%7    return wee Kday = = 0? 7:weekday}

In this version of the implementation, the first thing to know timeIntervalSince1970 is the current date and 1970-01-01 0 time difference, the same day is Thursday, so according to the timing of the week when the need to add 4, to ensure that the input year is less than 1970 is still valid, In other words, interval and days are likely to be negative, so modulo 7 after the remainder, plus 7 and modulo 7; Finally, in order to adjust the weekday by the previous agreement Monday starting from 1, the calculated 0 value is converted to 7, so there is the last line of "return weekday = = 2} 7:weekday ". Test "2016-01-17 23:58:00", the result is "7", there seems to be no problem, try a "1969-12-31 00:00:00", the result of 3 (previously said 1970-01-01 Thursday), but really wrong. Fortunately, the method is written in the night around 23:50, a 0 point to the next day, the problem came out, "2016-01-18 00:01:01" unexpectedly get out or 7, the same as the week of 17th! why???!!!

Debugging in the playground found only interval may have a problem, carefully Baidu and the official API document after the discovery, NSDate said time in memory is UTC time, that is, 0 time zones, when needed to display, will be displayed based on the time zone of the current system or the time zone specified in the code. Take "2016-01-18 00:01:01" as an example, the input value is naturally accompanied by the current time zone (China time Zone is East 8), converted to NSDate object becomes UTC time, that is "2016-01-17 16:01:01", the number of hours minus 8, and The time difference calculated by timeIntervalSince1970 is naturally 2016-01-17 to 1970-01-01. Knowing the problem, just modify the interval calculation to become "interval = Int (date!. timeIntervalSince1970) + Nstimezone.localtimezone (). Secondsfromgmt ", the revised version is:

Func getweekday (datetime:string)->int{let    datefmt = NSDateFormatter ()    Datefmt.dateformat = "Yyyy-mm-dd HH : Mm:ss "let    date = datefmt.datefromstring (dateTime)    date?". Description let    interval = Int (date!. timeIntervalSince1970) + Nstimezone.localtimezone (). Secondsfromgmt let days    = Int (interval/86400)//24*60*60 Let    weekday = ((days + 4)%7+7)%7    return weekday = = 0? 7:weekday}

Summarize this lesson: the small function can not let go of the test, and then the short code can not be taken for granted.

Swift returns the day of the week based on the date string

Related Article

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.