Skip to content

Instantly share code, notes, and snippets.

@Zhendryk
Created November 1, 2018 19:28
Show Gist options
  • Save Zhendryk/55ffc51c522c16df06aeebe6fd678e27 to your computer and use it in GitHub Desktop.
Save Zhendryk/55ffc51c522c16df06aeebe6fd678e27 to your computer and use it in GitHub Desktop.
Convenience methods for "date arithmetic" in easy to read and use formats
//
// Date+Intervals.swift
//
// Created by Zhendryk on 11/1/18.
// Copyright © 2018 Zhendryk. All rights reserved.
//
import Foundation
extension Date {
func getAmountOfTime(from date: Date, period: Calendar.Component) -> Int {
switch period {
case .year:
return years(from: date)
case .month:
return months(from: date)
case .weekOfMonth:
return weeks(from: date)
case .day:
return days(from: date)
case .hour:
return hours(from: date)
case .minute:
return minutes(from: date)
case .second:
return seconds(from: date)
case.nanosecond:
return nanoseconds(from: date)
default:
return 0
}
}
/// Returns the amount of years from another date
func years(from date: Date) -> Int {
return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
}
/// Returns the amount of months from another date
func months(from date: Date) -> Int {
return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
}
/// Returns the amount of weeks from another date
func weeks(from date: Date) -> Int {
return Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0
}
/// Returns the amount of days from another date
func days(from date: Date) -> Int {
return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
}
/// Returns the amount of hours from another date
func hours(from date: Date) -> Int {
return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
}
/// Returns the amount of minutes from another date
func minutes(from date: Date) -> Int {
return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
}
/// Returns the amount of seconds from another date
func seconds(from date: Date) -> Int {
return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
}
/// Returns the amount of nanoseconds from another date
func nanoseconds(from date: Date) -> Int {
return Calendar.current.dateComponents([.nanosecond], from: date, to: self).nanosecond ?? 0
}
/// Returns the custom time interval description from another date
func offset(from date: Date) -> String {
if years(from: date) > 0 { return "\(years(from: date))y" }
if months(from: date) > 0 { return "\(months(from: date))M" }
if weeks(from: date) > 0 { return "\(weeks(from: date))w" }
if days(from: date) > 0 { return "\(days(from: date))d" }
if hours(from: date) > 0 { return "\(hours(from: date))h" }
if minutes(from: date) > 0 { return "\(minutes(from: date))m" }
if seconds(from: date) > 0 { return "\(seconds(from: date))s" }
if nanoseconds(from: date) > 0 { return "\(nanoseconds(from: date))ns" }
return ""
}
func isBetween(date date1: Date, andDate date2: Date) -> Bool {
return date1.compare(self).rawValue * self.compare(date2).rawValue >= 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment