Last active
May 14, 2018 10:12
-
-
Save arpitjain03/843292a88df864542e0a0737805be7b3 to your computer and use it in GitHub Desktop.
This class can be used to get formatted Date Time as ago
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DateTimeAsAgo.swift | |
// DateTimeAsAgo | |
// | |
// Created by Arpit Jain on 14/05/18. | |
// Copyright (c) 2018 Arpit Jain . All rights reserved. | |
// | |
import Foundation | |
extension Date{ | |
/// Use this method to to get formatted Date Time as ago | |
/// | |
/// - Parameters: | |
/// - fromDate: Pass date from which you want to get fromatted date time as ago. | |
public func getDateTimeAsAgo(fromDate: Date = Date()) -> String { | |
let secondsFromDate = fromDate.secondsFrom(date: self) | |
if secondsFromDate < 60 { | |
return secondsFormatter() | |
} | |
let minutesFromDate = fromDate.minutesFrom(date: self) | |
if minutesFromDate < 60 { | |
return minutesFormatter(minutes: minutesFromDate) | |
} | |
let hoursFromDate = fromDate.hoursFrom(date: self) | |
if hoursFromDate < 24 { | |
return hoursFormatter(hours: hoursFromDate) | |
} | |
let daysFromDate = fromDate.daysFrom(date: self) | |
switch daysFromDate { | |
case 1: | |
return yesterdayFormatter() | |
case 2...6: | |
return daysFormatter(days: daysFromDate) | |
default: | |
break | |
} | |
let weeksFromDate = fromDate.weeksFrom(date: self) | |
let monthsFromDate = fromDate.monthsFrom(date: self) | |
switch monthsFromDate { | |
case 0: | |
return weeksFormatter(weeks: weeksFromDate) | |
case 1...11: | |
return monthsFormatter(months: monthsFromDate) | |
default: | |
break | |
} | |
let yearsFromDate = fromDate.yearsFrom(date: self) | |
return yearsFormatter(years: yearsFromDate) | |
} | |
} | |
// MARK: Formatter functions | |
func classicFormatterAgo(quantity: Int, unit: String) -> String { | |
var formattedString = "\(quantity) \(unit)" | |
if quantity > 1 { | |
formattedString += "s" | |
} | |
formattedString += " ago" | |
return formattedString | |
} | |
func secondsFormatter() -> String { | |
return "Just now" | |
} | |
func minutesFormatter(minutes: Int) -> String { | |
return classicFormatterAgo(quantity: minutes, unit: "minute") | |
} | |
func hoursFormatter(hours: Int) -> String { | |
return classicFormatterAgo(quantity: hours, unit: "hour") | |
} | |
func yesterdayFormatter() -> String { | |
return "Yesterday" | |
} | |
func daysFormatter(days: Int) -> String { | |
return classicFormatterAgo(quantity: days, unit: "day") | |
} | |
func weeksFormatter(weeks: Int) -> String { | |
return classicFormatterAgo(quantity: weeks, unit: "week") | |
} | |
func monthsFormatter(months: Int) -> String { | |
return classicFormatterAgo(quantity: months, unit: "month") | |
} | |
func yearsFormatter(years: Int) -> String { | |
return classicFormatterAgo(quantity: years, unit: "year") | |
} | |
// MARK: Extension of Date | |
private extension Date { | |
func yearsFrom(date:Date) -> Int{ | |
return Calendar.current.dateComponents([.year], from: date, to: self).year! | |
} | |
func monthsFrom(date:Date) -> Int{ | |
return Calendar.current.dateComponents([.month], from: date, to: self).month! | |
} | |
func weeksFrom(date:Date) -> Int{ | |
return Calendar.current.dateComponents([.weekOfYear], from: date, to: self).weekOfYear! | |
} | |
func daysFrom(date:Date) -> Int{ | |
return Calendar.current.dateComponents([.day], from: date, to: self).day! | |
} | |
func hoursFrom(date:Date) -> Int{ | |
return Calendar.current.dateComponents([.hour], from: date, to: self).hour! | |
} | |
func minutesFrom(date:Date) -> Int{ | |
return Calendar.current.dateComponents([.minute], from: date, to: self).minute! | |
} | |
func secondsFrom(date:Date) -> Int{ | |
return Calendar.current.dateComponents([.second], from: date, to: self).second! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment