DateHelper
A Swift Date extension for iOS, watchOS, tvOS, and macOS that creates or converts dates to or from strings. It can also compare, modify or extract date components and uses cached formatters for performance gains.
Capabilities
Convert from string
Convert to string
Compare dates
Adjust dates
Create dates for...
Forcing a week to start on monday
Time since...
Extracting components
Usage
Date from string
Use the initializer Date(fromString:String, format: DateFormatType)?
to create an optional date from a string.
The DateFormatType enum has a few predifined options as well as a tupple for providing a custom date format.
case isoYear: i.e. 1997
case isoYearMonth: i.e. 1997-07
case isoDate: i.e. 1997-07-16
case isoDateTime: i.e. 1997-07-16T19:20+01:00
case isoDateTimeSec: i.e. 1997-07-16T19:20:30+01:00
case isoDateTimeMilliSec: i.e. 1997-07-16T19:20:30.45+01:00
case dotNet: i.e. "/Date(1268123281843)/"
case rss: i.e. "Fri, 09 Sep 2011 15:26:08 +0200"
case altRSS: i.e. "09 Sep 2011 15:26:08 +0200"
case httpHeader: i.e. "Tue, 15 Nov 1994 12:45:26 GMT"
case standard: "EEE MMM dd HH:mm:ss Z yyyy"
case custom(String): a custom date format string
To String
There's four options for converting a date to string.
- date.toString(style: .shortWeekday)
- date.toString(format: .rss)
- date.toString(dateStyle: .short, timeStyle: .short)
- date.toStringWithRelativeTime()
1. String with style
Converts to string using a predefined style
The DateStyleType enum
2. String with format
Converts to string with specific or custom date formatters.
The DateFormatType enum has a few predifined options as well as a tupple for providing a custom date format.
case isoYear: i.e. 1997
case isoYearMonth: i.e. 1997-07
case isoDate: i.e. 1997-07-16
case isoDateTime: i.e. 1997-07-16T19:20+01:00
case isoDateTimeSec: i.e. 1997-07-16T19:20:30+01:00
case isoDateTimeMilliSec: i.e. 1997-07-16T19:20:30.45+01:00
case dotNet: i.e. "/Date(1268123281843)/"
case rss: i.e. "Fri, 09 Sep 2011 15:26:08 +0200"
case altRSS: i.e. "09 Sep 2011 15:26:08 +0200"
case httpHeader: i.e. "Tue, 15 Nov 1994 12:45:26 GMT"
case standard: "EEE MMM dd HH:mm:ss Z yyyy"
case custom(String): a custom date format string
Unicode Date Field Symbol Guide
Format | Description | Example |
---|---|---|
"y" | 1 digit min year | 1, 42, 2017 |
"yy" | 2 digit year | 01, 42, 17 |
"yyy" | 3 digit min year | 001, 042, 2017 |
"yyyy" | 4 digit min year | 0001, 0042, 2017 |
"M" | 1 digit min month | 7, 12 |
"MM" | 2 digit month | 07, 12 |
"MMM" | 3 letter month abbr. | Jul, Dec |
"MMMM" | Full month | July, December |
"MMMMM" | 1 letter month abbr. | J, D |
"d" | 1 digit min day | 4, 25 |
"dd" | 2 digit day | 04, 25 |
"E", "EE", "EEE" | 3 letter day name abbr. | Wed, Thu |
"EEEE" | full day name | Wednesday, Thursday |
"EEEEE" | 1 letter day name abbr. | W, T |
"EEEEEE" | 2 letter day name abbr. | We, Th |
"a" | Period of day | AM, PM |
"h" | AM/PM 1 digit min hour | 5, 7 |
"hh" | AM/PM 2 digit hour | 05, 07 |
"H" | 24 hr 1 digit min hour | 17, 7 |
"HH" | 24 hr 2 digit hour | 17, 07 |
"m" | 1 digit min minute | 1, 40 |
"mm" | 2 digit minute | 01, 40 |
"s" | 1 digit min second | 1, 40 |
"ss" | 2 digit second | 01, 40 |
"S" | 10th's place of fractional second | 123ms -> 1, 7ms -> 0 |
"SS" | 10th's & 100th's place of fractional second | 123ms -> 12, 7ms -> 00 |
"SSS" | 10th's & 100th's & 1,000's place of fractional second | 123ms -> 123, 7ms -> 007 |
3. String with date and time style
Converts to string using a DateFormatter.Style for date and time style plus an optional isRelative flag.
The DateFormatter.Style uses predefined formats for date and time.
4. String with relative time format
Converts to string using a customizable relative time format.
Use the optional strings:[RelativeTimeStringType:String]?
to customize the relative time strings
The RelativeTimeStringType enum keys are used for customizing the strings
Comparing Dates
Use the compare(_ comparison:DateComparisonType) -> Bool
function to compare a date against a type or a date.
The DateComparisonType enum can compare the date to a predetermined date, period or a custom date in a tupple. i.e. isLater(thanDate:Date)
Adjusting Dates
Use the adjust(_ component:DateComponentType, offset:Int) -> Date
to change a the date with an offset.
The DateComponentType enum is used for the components that can be adjusted.
Use the adjust(hour: Int?, minute: Int?, second: Int?, day: Int? = nil, month: Int? = nil) -> Date
function to change the date components.
Create Dates For...
Use the dateFor(_ type:DateForType) -> Date
function to create a new date for a specific occasion relative to the date.
The DateForType enum is used as a predetermined list of options.
Time Since In...
Use the since(_ date:Date, in component:DateComponentType) -> Int64
function to retrieve the component interval value compared to another date.
The DateComponentType enum is used for the vlaue to be retrieved.
Extracting Components
Use the component(_ component:DateComponentType) -> Int?
to get a specific component froma date.
The DateComponentType enum is used for the vlaue to be retrieved.
Related components to the date
Requirements
Language: Swift 4.0
Minimum: iOS 9, tvOS 9, watchOS 2, macOS 10.06
Installation
Carthage github "melvitax/DateHelper"
Cocoapods pod 'AFDateHelper', '~> 4.2.2'
Manually Include DateHelper.swift in your project
Author
Melvin Rivera