Manipulating dates with Swift 3.0

Source: Internet
Author: User
Tags dateformat locale


Joe, original link, original date: 2016-09-20
Translator: Cwift; proofreading: walkingway; final: CMB


When you want to rename at scale, a companion challenge is to ensure that all relevant documents must be updated synchronously. For example, as of September 20, 2016, Dateformatter's documentation is still not consistent with the version, citing the Swift 2.3-style API (translator Note: It is 2017, the document is still not updated ...) )。 Over time, these omissions will undoubtedly be corrected, here are some examples of usingDateandDateFormatterimplementing date formatting.



The current examples in the official documentation are as follows:


let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .mediumStyle
dateFormatter.timeStyle = .noStyle
 
let date = Date(timeIntervalSinceReferenceDate: 118800) // US English Locale (en_US)
dateFormatter.locale = Locale(localeIdentifier: "en_US") NSLog("%@", dateFormatter.stringFromDate(date)) // Jan 2, 2001 // French Locale (fr_FR)
dateFormatter.locale = Locale(localeIdentifier: "fr_FR") NSLog("%@", dateFormatter.stringFromDate(date)) // 2 janv. 2001 // Japanese Locale (ja_JP)
dateFormatter.locale = Locale(localeIdentifier: "ja_JP") NSLog("%@", dateFormatter.stringFromDate(date)) // 2001/01/02


In Swift 3.0, change to:


let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none


Attention.mediumStyleis simplified to.medium. This simplification conforms to the rules, and we know that the type isDateFormatter.Styleso there is no reason to repeat the word Style. The.none.noStylesame is true with substitution.



Now look at the changes that occurred while setting up the formatter's environment:


 
// US English Locale (en_US)
dateFormatter.locale = Locale(identifier: "en_US")
print(dateFormatter.string(from:date)) // Jan 2, 2001
 
// French Locale (fr_FR)
dateFormatter.locale = Locale(identifier: "fr_FR")
print(dateFormatter.string(from:date)) // 2 janv. 2001
 
// Japanese Locale (ja_JP)
dateFormatter.locale = Locale(identifier: "ja_JP")
print(dateFormatter.string(from:date)) // 2001/01/02


Once again, we see the simplification from theLocale(localeIdentifier:)toLocale(identifier:). The type name and its references are reduced. Similarly,DateFormatterthestringFromDatemethod has been simplified to astring(from:)complete method signaturestring(from:Date). Do you understand this pattern?



Continue toStringgenerate an object with a descriptive messageDate, and Apple's documentation shows an example:


 
let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd‘T‘HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(forSecondsFromGMT: 0) let string = "1996-12-19T16:39:57-08:00" let date = RFC3339DateFormatter.dateFromString(string)


In order to reduce the wordy and unnecessary vocabulary, the following wording was obtained:


 
let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(identifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd‘T‘HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0) let string = "1996-12-19T16:39:57-08:00" let date = RFC3339DateFormatter.date(from:string)


TimeZoneThe constructor removes extraneous three characters (for), and as expected, thedateFromStringmethod becomesdate(from:).


Rule of thumb


A common rule for converting from Swift 2 to Swift 3 o'clock is to remove extra words. If you have been used to writingformatter.timeStyle = .fullStyle, now you have to get used toformatter.timeStyle = .fullthe wording. If you seesomeTypeFromAnotherType()this notation, it may have been replaced bysomeType(from:AnotherType). Speaking of my experience, after using Swift 3 months, and then returning to Swift 2, I feel that the name is too lengthy, which I prefer in the context of a detailed self-explanatory language style. But once you've learned Swift, you'll embrace Hemingway and avoid Tolstoy.



Happy Swift!


This article is translated by Swiftgg translation Group, has obtained the author's translation authorization, the latest article please visit http://swift.gg.


Manipulating dates with Swift 3.0


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.