-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDateExtensions.swift
More file actions
executable file
·111 lines (94 loc) · 3.57 KB
/
Copy pathDateExtensions.swift
File metadata and controls
executable file
·111 lines (94 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// DateExtensions.swift
// ServiceStackClient
//
// Created by Demis Bellot on 1/30/15.
// Copyright (c) 2015 ServiceStack LLC. All rights reserved.
//
import Foundation
import PromiseKit
public extension Date {
public init(dateString:String, format:String="yyyy-MM-dd") {
let fmt = DateFormatter()
fmt.timeZone = NSTimeZone.default
fmt.dateFormat = format
let d = fmt.date(from: dateString)
self.init(timeInterval:0, since:d!)
}
public init(year:Int, month:Int, day:Int) {
let c = NSDateComponents()
c.year = year
c.month = month
c.day = day
let gregorian = NSCalendar(identifier:NSCalendar.Identifier.gregorian)
let d = gregorian?.date(from: c as DateComponents)
self.init(timeInterval:0, since:d!)
}
public func components() -> DateComponents {
let components = NSCalendar.current.dateComponents(
[Calendar.Component.day, Calendar.Component.month, Calendar.Component.year],
from: self as Date)
return components
}
public var year:Int {
return components().year!
}
public var month:Int {
return components().month!
}
public var day:Int {
return components().day!
}
public var shortDateString:String {
let fmt = DateFormatter()
fmt.timeZone = NSTimeZone.default
fmt.dateFormat = "yyyy-MM-dd"
return fmt.string(from: self as Date)
}
public var dateAndTimeString:String {
let fmt = DateFormatter()
fmt.timeZone = NSTimeZone.default
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
return fmt.string(from: self as Date)
}
public var jsonDate:String {
let unixEpoch = Int64(self.timeIntervalSince1970 * 1000)
return "/Date(\(unixEpoch)-0000)/"
}
public var isoDateString:String {
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
return dateFormatter.string(from: self as Date).appendingFormat("Z")
}
public static func fromIsoDateString(_ string:String) -> Date? {
let isUtc = string.hasSuffix("Z")
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
dateFormatter.timeZone = isUtc ? TimeZone(abbreviation: "UTC") : TimeZone.ReferenceType.local
dateFormatter.dateFormat = string.length == 19 || (isUtc && string.length == 20)
? "yyyy-MM-dd'T'HH:mm:ss"
: "yyyy-MM-dd'T'HH:mm:ss.SSSSSSS"
return isUtc
? dateFormatter.date(from: string[0..<string.length-1])
: dateFormatter.date(from: string)
}
}
public func >(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs as Date) == ComparisonResult.orderedDescending
}
public func >=(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs as Date) == ComparisonResult.orderedDescending
|| lhs == rhs
}
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs as Date) == ComparisonResult.orderedAscending
}
public func <=(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs as Date) == ComparisonResult.orderedAscending
|| lhs == rhs
}
public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs as Date) == ComparisonResult.orderedSame
}