Created
November 16, 2022 11:54
-
-
Save between40and2/0f8398c14d4430e1c9bfd8c2bfc3ee20 to your computer and use it in GitHub Desktop.
WWDC 2021 What's New in Foundation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func parsingDates() { | |
let date = Date.now | |
let format = Date.FormatStyle().year().day().month() | |
let formatted = date.formatted(format) // "Jun 7, 2021" | |
if let date = try? Date(formatted, strategy: format) { | |
// example: 2021-06-07 07:00:00 +0000 | |
print(date) | |
} | |
} | |
parsingDates() | |
func parsingDatesStrategies() { | |
let strategy = Date.ParseStrategy( | |
format: "\(year: .defaultDigits)-\(month: .twoDigits)-\(day: .twoDigits)", | |
timeZone: TimeZone.current) | |
if let date = try? Date("2021-06-07", strategy: strategy) { | |
// date is 2021-06-07 07:00:00 +0000 | |
print(date) | |
} | |
} | |
parsingDatesStrategies() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment