RelativeFormatter 0.8.3

RelativeFormatter 0.8.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2016
SPMSupports SPM

Maintained by Bitomule.



  • By
  • David Collado Sela and David Santana

RelativeFormatter

Date swift extension to format dates according to current date.

Features

  • [x] Format Date as Time ago
  • [x] Format Date as Time ahead
  • [x] Format using idiomatic style (today,yesterday,tomorrow)
  • [x] Set format precision (years,months,weeks,days,hours,minutes and seconds)

Requirements

  • iOS 8.0+ / Mac OS X 10.9+
  • Xcode 8.0
  • Swift 3

Installation

Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks.

How To Use

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”

Languages

RelativeFormatter includes localization for:

  • [x] English
  • [x] Spanish
  • [x] French

If you can to include a new language please create a pull request