Skip to content

Instantly share code, notes, and snippets.

@astericky
Forked from alexpaul/Date.swift
Created October 7, 2020 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astericky/0468627aca64c7c849a940609733f4bd to your computer and use it in GitHub Desktop.
Save astericky/0468627aca64c7c849a940609733f4bd to your computer and use it in GitHub Desktop.
Using Date in Swift, using ISO8601DateFormatter() and DateFormatter()
// Date extension
extension Date {
static func getStringFromDate(date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
let dateString = dateFormatter.string(from: date)
return dateString
}
static func getDateFromString(dateString: String) -> Date? {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime,
.withDashSeparatorInDate,
.withFullDate,
.withFractionalSeconds,
.withColonSeparatorInTimeZone]
guard let date = formatter.date(from: dateString) else {
return nil
}
return date
}
// get an ISO timestamp
static func getISOTimestamp() -> String {
let isoDateFormatter = ISO8601DateFormatter()
let timestamp = isoDateFormatter.string(from: Date())
return timestamp
}
}
extension String {
// create a formatted date from ISO
// e.g "MMM d, yyyy hh:mm a"
// e.g usage addedAt.formattedDate("MMM d, yyyy")
public func formatISODateString(dateFormat: String) -> String {
var formatDate = self
let isoDateFormatter = ISO8601DateFormatter()
if let date = isoDateFormatter.date(from: self) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
formatDate = dateFormatter.string(from: date)
}
return formatDate
}
// e.g usage createdAt.date()
public func date() -> Date {
var date = Date()
let isoDateFormatter = ISO8601DateFormatter()
if let isoDate = isoDateFormatter.date(from: self) {
date = isoDate
}
return date
}
}
// testing Date extension
let date = Date.getDateFromString(dateString: "2018-11-02T06:02:45+00:00")
print(date?.description ?? "invalid date")
if let date = date {
let dateString = Date.getStringFromDate(date: date)
print(dateString)
}
// Date resources
// https://developer.apple.com/documentation/foundation/iso8601dateformatter/1643324-formatoptions
// http://nsdateformatter.com/
// https://developer.apple.com/documentation/foundation/iso8601dateformatter
// https://developer.apple.com/documentation/foundation/dateformatter
// https://www.unixtimestamp.com/index.php
// making a Date from a String using DateFormatter()
let dateString = "2016-12-01"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: dateString)
print(date ?? "not a date")
dateFormatter.dateFormat = "EEEE, MMMM d, yyyy"
let formatString = dateFormatter.string(from: date!)
print(formatString)
// making a date from a TimeInterval
let timeInterval: TimeInterval = 1544378400000 // TimeInterval
let dateFormatter = DateFormatter()
let date = Date(timeIntervalSinceNow: timeInterval/1000000) // milli to seconds
// using DateFormatter()
dateFormatter.dateFormat = "MMMM, dd, yyyy h:mm a"
let dateString = dateFormatter.string(from: date)
print(dateString) // December, 27, 2018 8:08 AM
// using ISO8601DateFormatter() to make a timestamp for the current date
let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.formatOptions = [.withInternetDateTime,
.withDashSeparatorInDate,
.withFullDate,
.withFractionalSeconds,
.withColonSeparatorInTimeZone]
isoDateFormatter.timeZone = TimeZone.current
let timestamp = isoDateFormatter.string(from: Date())
print(timestamp) // 2018-12-09T11:08:48-05:00
// convert ISO timestamp string to formatted date
let timestampString = "2018-12-09T11:08:48-05:00"
if let date = isoDateFormatter.date(from: timestampString) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM, dd, yyyy h:mm a"
let dateFormattedString = dateFormatter.string(from: date)
print(dateFormattedString) // December, 09, 2018 11:08 AM
} else {
print("not a valid date")
}
// if ISO8601 comes in this format "2014-12-10T15:18:20.704000Z" be sure to using .withFractionalSeconds as a
// format option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment