X

How To Format Date Time In Swift

Swift Date Time Formatting


Overview Patterns Swift 4 Date Time Examples  Swift 3 Date Time Examples


Overview

Swift is a programming language for macOS, iOS, watchOS and tvOS.  Chances are if you are developing a mobile app for iphones, ipads or iwatches, you’ll need to learn swift.  Date Time Formatting in Swift is based off of the DateFormatter class which can be used to manipulate dates and times. An Instance of DateFormatter creates a string representation of NSDate objects, and can also convert textual representations of dates and times into NSDate objects.

Patterns

Characters Example Description
Year
y 2018 Year, no padding
yy 18 Year, two digits (padding with a zero if necessary)
yyyy 2018 Year, minimum of four digits (padding with zeros if necessary)
Quarter
Q 2 The quarter of the year. Use QQ if you want zero padding.
QQQ Q2 Quarter including “Q”
QQQQ 2nd quarter Quarter spelled out
Month
M 11 The numeric month of the year. A single M will use ‘1’ for January.
MM 11 The numeric month of the year. A double M will use ’01’ for January.
MMM Nov The shorthand name of the month
MMMM November Full name of the month
MMMMM N Narrow name of the month
Day
d 26 The day of the month. A single d will use 1 for January 1st.
dd 26 The day of the month. A double d will use 01 for January 1st.
F 4th Wednesday in December The day of week in the month
E Weds The day of week in the month
EEEE Wednesday The full name of the day
EEEEE W The narrow day of week
Hour
h 5 The 12-hour hour.
hh 05 The 12-hour hour padding with a zero if there is only 1 digit
H 17 The 24-hour hour.
HH 17 The 24-hour hour padding with a zero if there is only 1 digit.
a PM AM / PM for 12-hour time formats
Minute
m 25 The minute, with no padding for zeroes.
mm 25 The minute with zero padding.
Second
s 1 The seconds, with no padding for zeroes.
ss 01 The seconds with zero padding.
Time Zone
zzz EST The 3 letter name of the time zone. Falls back to GMT-08:00 (hour offset) if the name is not known.
zzzz Eastern Standard Time The expanded time zone name, falls back to GMT-08:00 (hour offset) if name is not known.
zzzz CST-04:00 Time zone with abbreviation and offset
Z -0400 RFC 822 GMT format. Can also match a literal Z for Zulu (UTC) time.
ZZZZZ -04:00 ISO 8601 time zone format

Date Time Formatting in Swift 4

 

func convertDateFormatter(date: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"//this your string date format
    dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
    dateFormatter.locale = Locale(identifier: "your_loc_id")
    let convertedDate = dateFormatter.date(from: date)

    guard dateFormatter.date(from: date) != nil else {
        assert(false, "no date from string")
        return ""
    } 

    dateFormatter.dateFormat = "yyyy MMM HH:mm EEEE"///this is what you want to convert format
    dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
    let timeStamp = dateFormatter.string(from: convertedDate!)

    return timeStamp
}

 

Date Time formatting in Swift 3

 

func convertDateFormater(date: String) -> String {   
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    dateFormatter.timeZone = NSTimeZone(name: "UTC")

    guard let date = dateFormatter.dateFromString(date) else {
        assert(false, "no date from string")
        return ""
    }

    dateFormatter.dateFormat = "yyyy MMM EEEE HH:mm"
    dateFormatter.timeZone = NSTimeZone(name: "UTC")
    let timeStamp = dateFormatter.stringFromDate(date)

    return timeStamp
}