Skip to content

Instantly share code, notes, and snippets.

@agiguere
Last active October 12, 2018 18:58
Show Gist options
  • Save agiguere/12c538e9ab61e611a872f509b70026ac to your computer and use it in GitHub Desktop.
Save agiguere/12c538e9ab61e611a872f509b70026ac to your computer and use it in GitHub Desktop.
Swift Date Functions
//
// Date+Extension.swift
//
// Created by Alexandre Giguere on 2016-05-16.
//
import Foundation
extension Date {
public init?(jsonDate: String) {
if jsonDate.count == 26 {
let timeStamp = jsonDate[jsonDate.index(jsonDate.startIndex, offsetBy: 6)..<jsonDate.index(jsonDate.endIndex, offsetBy: -7)]
if let timeInterval = Double(timeStamp) {
self.init(timeIntervalSince1970: (timeInterval / 1000.0))
} else {
return nil
}
} else if jsonDate.count == 21 {
let timeStamp = jsonDate[jsonDate.index(jsonDate.startIndex, offsetBy: 6)..<jsonDate.index(jsonDate.endIndex, offsetBy: -2)]
if let timeInterval = Double(timeStamp) {
self.init(timeIntervalSince1970: (timeInterval / 1000.0))
} else {
return nil
}
} else {
return nil
}
}
public var jsonString: String {
let time = Int64(self.timeIntervalSince1970 * 1000.0)
return "/Date(\(time)+0000)/"
}
public static func today12AM() -> Date {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let currentDateComponents = calendar.dateComponents([.year, .month, .day], from: Date())
return calendar.date(from: currentDateComponents)!
}
public mutating func setTimeTo12AM() {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let currentDateComponents = calendar.dateComponents([.year, .month, .day], from: self)
self = calendar.date(from: currentDateComponents)!
}
public func settingTimeTo12AM() -> Date {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let currentDateComponents = calendar.dateComponents([.year, .month, .day], from: self)
return calendar.date(from: currentDateComponents)!
}
// Returns the amount of years from another date
public 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
public 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
public func weeks(from date: Date) -> Int {
return Calendar.current.dateComponents([.weekOfYear], from: date, to: self).weekOfYear ?? 0
}
// Returns the amount of days from another date
public 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
public 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
public 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
public func seconds(from date: Date) -> Int {
return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment