This might help you also formatting your dates: Convert Unix TimeStamp to NSDate in Swift If you have a timestamp '/Date(000-0800)' and you need to convert it to an NSDate, you can use the following extension. The advantage of using the DateFormatter approach is that dates can easily be displayed or evaluated with other like dates, just keep in mind that the timezone offset will be displayed at the end of the date with a + or - and then the hour offset to GMT. For example, my timezone offset is -0500 for America/Chicago at the time of writing this.
Working with dates in iOS10 can be a bit confusing. There are several different types associated with dates. You can use them for telling time, scheduling time and setting the differences between two times. In this lesson, let’s look at the different types of dates. I’ll use a playground to do this. You can download here the full lesson as a zip file if you wish, or just type along. The file has the entire lesson, so if you load it on iPad Playgrounds, it will format like this post.
The simplest date representation is from the type
Date
. For the current date, instantiate a variable with the constructor like this:Prints
2016-11-10 11:58:36 +0000
to the console.Time Intervals
On the other hand, you might want a measure of time. For this, use the type
TimeInterval
. This is a measure of time using seconds. They are of Double
type. I’ll set a few constants using time intervals for day, hour and minute.Swift Date Format Cheat Sheet
You can set a date based on the time interval. If you want a time interval different from now, use the constructor
Date(timeIntervalSinceNow:)
Gives you a date one minute from now
2016-11-10 11:59:36 +0000
. If you want a date where you add or subtract a time interval, use Date(timeInterval:since:)
Swift Date Format Am Pm
Gives us a date an hour from now:
2016-11-10 12:59:36 +0000
.The date looks ugly. Let’s make the date look better with a date formatter. Start with initializing a date formatter
Date formatters need you to set a style for the date using the
dateStyle
property and time property timeStyle
. You have a choice of .full
.long
, .medium
, .short
, and .none
Receiptbox 2 2 – keep track of your receipts.There’s a function on date formatters,
string(from:)
that returns a string with the formatted date. Pixatool 1 35 – create 8bit pixel style images css.Prints
Nov 10, 2016, 6:59 AM
. Change the styles to .full
Swift Date Format Yyyy-mm-dd
You’ll get a full date like
Thursday, November 10, 2016 at 6:59:36 AM Central Standard Time
. Change the styles to .short
You’ll find printed something like
11/10/16, 7:20 AM
on the console. If you want only a date or a time, use .none
Prints just the time
Date formatters are locale dependent. They use the current locale by default. You can change the locale and use that locale’s format. For example If I want the time for some where with really authentic pizza,
7:20:53 AM
Date formatters are locale dependent. They use the current locale by default. You can change the locale and use that locale’s format. For example If I want the time for some where with really authentic pizza,
Prints to the console the Italian date format
10 nov 2016, 06:59:36
This is good for relative dates from a time frame. What if you want an absolute date? That’s where
DateComponents
comes into play. A date component provides a way to add components for a date. Initialize DateComponents
like this:You assign to date componenets the date
Date components are optional values. When a value is not set it returns
nil
. If you have a nil
value for one component, operations on the dateComponents
object will cause an nil
error. There is a property isValidDate
to confirm if you have all the information.Prints
false
. You don’t have enough components yet. Include the calendar used in a date componenets object. Usually this is the current calendar for the system. A calendar fills in the rest of the information the date componenets needs to make a valid date. You can get the current calendar using Calendar.current
The console prints
true.
The date components have a property date
if you need a value of type Date
. Of course you can format this way, changing the components back into dates.Prints
You can also convert a date to date components. Font chooser. However, you do it from the current calendar, not the
04 nov 2016, 13:08:00
You can also convert a date to date components. Font chooser. However, you do it from the current calendar, not the
Date
type. Make a set of Calendar
components. For most cases, include the component .calendar
to avoid a nil
in the components.Using the current calendar, get the components
Print the date using the date formatter.
Prints the date
Date components can be changed. The date components are all optional type
10 nov 2016, 06:19:53
.Date components can be changed. The date components are all optional type
Int
. While it is easier to change them using Date
and TimeInterval
, you might need to do it this way if you’ve created a series of controls in your UI to set a date. For example, to add five days do this:The console prints
Each of these types have their purposes.
15 nov 2016, 06:19:53
.Each of these types have their purposes.
Date
is the simplest and best for the current date and dates based on the current date. To show an interval in seconds, use the TimeInterval
. To work with indivdual date components, use DateComponents
. You’ll find each of these in other API’s as well. For example, all three have purposes in the UserNotification
framework as event triggers. Learing how to use all three will help you display time and schedule time throughout iOS.The Whole code
You can download here the full lesson as a zip file.