-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSDateAdditions.swift
More file actions
134 lines (101 loc) · 3.77 KB
/
NSDateAdditions.swift
File metadata and controls
134 lines (101 loc) · 3.77 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Additions.swift
// CalendarLogic
//
// Created by Lancy on 01/06/15.
// Copyright (c) 2015 Lancy. All rights reserved.
//
import Foundation
let dateFormatter = NSDateFormatter()
extension NSDate {
class func date(day: Int, month: Int, year: Int) -> NSDate {
dateFormatter.dateFormat = "dd/MM/yyyy"
let dayString = String(format: "%02d", day)
let monthString = String(format: "%02d", month)
let yearString = String(format: "%04d", year)
return dateFormatter.dateFromString(dayString + "/" + monthString + "/" + yearString)!
}
class func date(fromString: String, dateFormat: String) -> NSDate {
dateFormatter.dateFormat = dateFormat
return dateFormatter.dateFromString(fromString)!
}
func string(dateFormat: String) -> String {
dateFormatter.dateFormat = dateFormat
return dateFormatter.stringFromDate(self)
}
func dateByAdding(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int) -> NSDate {
let dateComponent = NSDateComponents()
dateComponent.year = year
dateComponent.month = month
dateComponent.day = day
dateComponent.hour = hour
dateComponent.minute = minute
dateComponent.second = second
return NSCalendar.currentCalendar().dateByAddingComponents(dateComponent, toDate: self, options: NSCalendarOptions(0))!
}
var startOfDay: NSDate {
let components = self.components
components.hour = 0
components.minute = 0
components.second = 0
return NSCalendar.currentCalendar().dateFromComponents(components)!
}
var endOfTheDay: NSDate {
let components = self.components
components.hour = 23
components.minute = 59
components.second = 59
return NSCalendar.currentCalendar().dateFromComponents(components)!
}
var firstDayOfTheMonth: NSDate {
var date: NSDate?
NSCalendar.currentCalendar().rangeOfUnit(.CalendarUnitMonth, startDate:&date , interval: nil, forDate: self)
return date!
}
var firstDayOfPreviousMonth: NSDate {
return firstDay(false)
}
var firstDayOfFollowingMonth: NSDate {
return firstDay(true)
}
var monthDayAndYearComponents: NSDateComponents {
let components: NSCalendarUnit = .CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay
return NSCalendar.currentCalendar().components(components, fromDate: self)
}
var weekDay: Int {
return components.weekday
}
var numberOfDaysInMonth: Int {
return NSCalendar.currentCalendar().rangeOfUnit(.CalendarUnitDay, inUnit: .CalendarUnitMonth, forDate: self).length
}
var day: Int {
return components.day
}
var month: Int {
return components.month
}
var year: Int {
return components.year
}
var minute: Int {
return components.minute
}
var second: Int {
return components.second
}
var hour: Int {
return components.hour
}
//MARK: Private variable and methods.
private var components: NSDateComponents {
let calendarUnit = NSCalendarUnit(UInt.max)
let components = NSCalendar.currentCalendar().components(calendarUnit, fromDate: self)
return components
}
private func firstDay(followingMonth: Bool) -> NSDate {
let dateComponent = NSDateComponents()
dateComponent.month = followingMonth ? 1: -1
let date = NSCalendar.currentCalendar().dateByAddingComponents(dateComponent, toDate: self, options: NSCalendarOptions(0))
return date!.firstDayOfTheMonth
}
}