others-how to do date format with swift

1. Purpose

In this post, I would show some ways to do date format with swift language.

2. Environment

  • Mac OS 10.15
  • Swift 5
  • Xcode 12

3. The solution

3.1 The DateFormatter

We can use DateFormatter to do this job, what is a DateFormatter in swift?

A formatter that converts between dates and their textual representations.

Instances of DateFormatter create string representations of NSDate objects, and convert textual representations of dates and times into NSDate objects. For user-visible representations of dates and times, DateFormatter provides a variety of localized presets and configuration options. For fixed format representations of dates and times, you can specify a custom format string.

When working with date representations in ISO 8601 format, use ISO8601DateFormatter instead.

To represent an interval between two NSDate objects, use DateIntervalFormatter instead.

To represent a quantity of time specified by an NSDateComponents object, use DateComponentsFormatter instead.

Classic date format code example:

let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(identifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
 
/* 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an offset of -08:00 from UTC (Pacific Standard Time) */
let string = "1996-12-19T16:39:57-08:00"
let date = RFC3339DateFormatter.date(from: string)

3.2 How to format date as format yyyy-MM-dd HH:mm:ss

func getNowTheTime() -> String {
    // create the date formatter
    let dateFormatter = DateFormatter()
    // setup format string for the formatter
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    // format the current date and time by the date formatter
    let dateStr = dateFormatter.string(from: Date())
    return dateStr
}

3.3 How to format date as format dd/MM/yy HH:mm

func getTimeFormatForFileExtension() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd/MM/yy HH:mm"
    let dateStr = dateFormatter.string(from: Date())
    return dateStr
}

3.4 The formatter description

About the format description:

Character description

(:)

Time separator. In some locales, other characters can be used to represent the time separator. The time separator separates hours, minutes, and seconds when formatting time values. The actual character used as the time separator in the formatted output is determined by the current culture value of your application.

(/)

Date separator. In some locales, other characters can be used to represent the date separator. The date separator separates the day, month, and year when formatting date values. The actual character used as the date separator in the formatted output is determined by the current culture of your application.

(%)

Used to indicate that no matter what letter is trailing, subsequent characters should be read in single letter format. It is also used to indicate that the single letter format should be read in a user-defined format. For more details, see below.

d

Display the day as a number without leading zeros (such as 1). If this is the only character in a user-defined number format, use %d.

dd

Display the day as a number with leading zeros (such as 01).

EEE

Display the day as an abbreviated form (for example, Sun).

EEEE

Display the day as a full name (for example, Sunday).

M

Display the month as a number without leading zeros (for example, January is represented as 1). If this is the only character in a user-defined number format, use %M.

MM

Display the month as a number with leading zeros (for example, 01/12/01).

MMM

Display the month as an abbreviated form (for example, Jan).

MMMM

Display the month as the full month name (for example, January).

gg

Display the era/epoch string (e.g. A.D.)

h

Use the 12-hour clock to display the hour as a number without leading zeros (for example, 1:15:15 PM). If this is the only character in a user-defined number format, use %h.

hh

Use the 12-hour clock to display the hour as a number with leading zeros (for example, 01:15:15 PM).

H

Use the 24-hour clock to display the hour as a number without leading zeros (for example, 1:15:15). If this is the only character in a user-defined number format, use %H.

HH

Use the 24-hour clock to display the hour as a number with leading zeros (for example, 01:15:15).

m

Display the minutes as a number without leading zeros (for example, 12:1:15). If this is the only character in a user-defined number format, use %m.

mm

Display the minutes as a number with leading zeros (for example, 12:01:15).

s

Display the seconds as a number without leading zeros (for example, 12:15:5). If this is the only character in a user-defined number format, use %s.

ss

Display the seconds as a number with leading zeros (for example, 12:15:05).

f

Display the fractional part of the second. For example, ff will be displayed to the nearest hundredth of a second, and ffff will be displayed to the nearest ten-thousandth of a second. Up to seven f symbols can be used in the user-defined format. If this is the only character in a user-defined number format, use %f.

t

Use the 12-hour clock and display an uppercase A for any hour before noon, and an uppercase P for any hour between noon and 11:59 P.M. If this is the only character in a user-defined number format, use %t.

tt

For locales that use the 12-hour clock, the uppercase AM is displayed for any hour before noon, and the uppercase PM is displayed for any hour between noon and 11:59 P.M.

For locales that use the 24-hour clock, no characters are displayed.

y

Display the year (0-9) as a number without leading zeros. If this is the only character in a user-defined number format, use %y.

yy

Display the year (if applicable) in two-digit format with leading zeros.

yyy

The year is displayed in a four-digit format.

yyyy

The year is displayed in a four-digit format.

z

Display the time zone offset without leading zeros (such as -8). If this is the only character in a user-defined number format, use %z.

zz

Display the time zone offset with leading zeros (e.g. -08)

zzz

Display the full time zone offset (e.g. -08:00)

4. Summary

In this post, I demonstrated how to do date format with swift by using DateFormatter .