Latest0.8.3
Homepagehttps://github.com/bitomule/RelativeFormatter
LicenseMIT
Platformsios 8.0, osx 10.9, watchos 2.0, tvos 9.0, requires ARC
Authors,

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.

CocoaPods

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

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

Latest podspec

{
    "name": "RelativeFormatter",
    "version": "0.8.3",
    "summary": "Date swift extension to format dates relative to current date.",
    "description": "RelativeFormatter adds the relativeFormatted function to Date allowing you to get.nn* Time ago formatted dates "2 days ago"n* Time in the future formatted dates "In 2 days"",
    "homepage": "https://github.com/bitomule/RelativeFormatter",
    "license": "MIT",
    "authors": {
        "David Collado Sela": "[email protected]",
        "David Santana": "[email protected]"
    },
    "social_media_url": "http://twitter.com/bitomule",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/bitomule/RelativeFormatter.git",
        "tag": "0.8.3"
    },
    "source_files": "Source/*.swift",
    "resources": "Source/RelativeFormatter.bundle",
    "requires_arc": true
}

Pin It on Pinterest

Share This