Date and Time in iOS

The song says time is on my side, but I sometimes I wonder about that when trying to work with the time types in iOS.
Let me show you some of the time types. In Xcode, Open up a blank playground. There are two primary types for working with time: TimeInterval and Date. Add this to the playground:

var minute:TimeInterval = 60.0 // seconds in a minute

We can measure a length of time in seconds in a time interval. TimeInterval is an alias for Double, so you can use doubles anywhere you use time intervals. I can, for example, add a constant for an hour and day like this:

let hour = 3600.0 or minute * 60.0
let day = hour * 24.0

The simplest date representation is from the type Date. For the current date, instantiate a variable with the constructor like this:

var date = Date()

We see the date in the viewer.
You can set a date based on the time interval. If you want a time interval different from now, use the constructor

date = Date(timeIntervalSinceNow: hour)

Gives you a date one hour from now. If you want a date where you add or subtract a time interval, use

date = Date(timeInterval: day + hour, since: date)

Gives us a date 25 hours from our date value:
While the date looks nice in the playground viewer, try printing it.

print(date)

The date looks ugly. Unless you are close to zero degrees Longitude, You’ll also notice it has a different time than the viewers. That’s Greenwich Mean time which you can tell by the +0000. Playgrounds localizes the time for you..

You use a date formatter to make the time and date look good as a string. Start with initializing a date formatter

let dateFormatter = DateFormatter()

You set a style for the date and time using the dateStyle property and timeStyle property. Type this.

dateFormatter.dateStyle = .

You’ll see all the time styles. Select .medium

dateFormatter.dateStyle = .medium

Set the timeStyle to .short

dateFormatter.timeStyle = .short

There’s a method string:from: on date formatters, that returns a string with the formatted date.

var string = dateFormatter.string(from: date)

And print that

print(string)

Run that and you get a date, using localized time.
Change the styles to .full

dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .full

If you want only a date or a time, use .none. If I just want the time, I’ll change the dateStyle to none.

dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .medium

And you can get a date by changing time to .none. I’ll change the date back to .full though.

While DateFormatters use the current locale by default, you can change the locale, changing the language and date time format. For example If I want the time and date for At Pizzeria Brandi in Naples for really authentic pizza, I change the locale to Italy using the locale property:

dateFormatter.locale = Locale(identifier: "it-it")

Prints to the console the Italian date format in Italian, but does not change the time zone
Date and time Interval make up the basic building blocks of all timekeeping. Date will give you a date, and let you change it relative to a TimeInvterval. When you have a date you can change the format, including localization, using a date formatter.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.