Date swift extension to format dates according to current date.
- Format Date as Time ago
- Format Date as Time ahead
- Format using idiomatic style (today,yesterday,tomorrow)
- Set format precision (years,months,weeks,days,hours,minutes and seconds)
- iOS 8.0+ / Mac OS X 10.9+
- Xcode 8.0
- Swift 3
Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks.
CocoaPods is a dependency manager for Cocoa projects.
CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:
$ gem install cocoapods
To integrate RelativeFormatter into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'RelativeFormatter'
Then, run the following command:
$ pod install
RelativeFormatter is just an NSDate extension, you can use it with any NSDate object:
There’s only one function to call:
relativeFormatted(idiomatic:Bool=false,precision:Precision=Precision.Second)->String
Both parameters aren’t required.
- idiomatic:Bool
This parameter is false by default and allows you to use idiomatic date format or just numbers.
Example:
//oldDate is yesterday date
oldDate.relativeFormatted()
//outputs
// “1 day ago”
oldDate.relativeFormatted(idiomatic:true)
//outputs
// “yesterday”
- precision:Precision
Precision parameter allows you to define the format precission. Default value is seconds.
Example:
todayDate.relativeFormatted(precision:Precision.Year)
//outputs
// “this year”
todayDate.relativeFormatted(precision:Precision.Month)
//outputs
// “this month”
todayDate.relativeFormatted(precision:Precision.Day)
//outputs
// “today”
todayDate.relativeFormatted(precision:Precision.Hour)
//outputs
// “3 hours ago”
You can always use relativeFormatted with default parameters. If you have an NSDate representing a date 2 months ago just use:
oldDate.relativeFormatted()
And you'll get:
"2 months ago"
It also works for ahead dates (date in 3 years):
futureDate.relativeFormatted()
will return:
"In 3 years"
RelativeFormatter includes localization for:
- English
- Spanish
- French
If you can to include a new language please create a pull request