@@ -105,15 +105,21 @@ public extension Date {
105
105
/// - parameter unit: A TimeUnit value.
106
106
///
107
107
/// - returns: A new Moment instance.
108
+ ///
108
109
func dateAtEndOf( _ unit: Calendar . Component ) -> Date {
109
- inDefaultRegion ( ) . dateAtEndOf ( unit) . date
110
+ switch unit {
111
+ case . day: return inDefaultRegion ( ) . dateAt ( . endOfDay) . date
112
+ case . month: return inDefaultRegion ( ) . dateAt ( . endOfMonth) . date
113
+ default : return inDefaultRegion ( ) . dateAtEndOf ( unit) . date
114
+ }
110
115
}
111
116
112
- /// Return a new DateInRegion that is initialized at the end of the specified components
113
- /// executed in order.
117
+ /// Return a new `` DateInRegion`` that is initialized at the end of the
118
+ /// specified components executed in order.
114
119
///
115
120
/// - Parameter units: sequence of transformations as time unit components
116
121
/// - Returns: new date at the end of the passed components, intermediate results if fails.
122
+ ///
117
123
func dateAtEndOf( _ units: [ Calendar . Component ] ) -> Date {
118
124
units. reduce ( self ) { ( currentDate, currentUnit) -> Date in
119
125
return currentDate. dateAtEndOf ( currentUnit)
@@ -136,7 +142,7 @@ public extension Date {
136
142
/// - secs: sec to set (`nil` to leave it unaltered)
137
143
/// - ms: milliseconds to set (`nil` to leave it unaltered)
138
144
/// - options: options for calculation
139
- /// - Returns: new altered `DateInRegion` instance
145
+ /// - Returns: new altered `` DateInRegion` ` instance
140
146
func dateBySet( hour: Int ? , min: Int ? , secs: Int ? , ms: Int ? = nil , options: TimeCalculationOptions = TimeCalculationOptions ( ) ) -> Date ? {
141
147
let srcDate = DateInRegion ( self , region: SwiftDate . defaultRegion)
142
148
return srcDate. dateBySet ( hour: hour, min: min, secs: secs, ms: ms, options: options) ? . date
@@ -165,6 +171,7 @@ public extension Date {
165
171
/// - count: value of the offset.
166
172
/// - component: component to offset.
167
173
/// - Returns: new altered date.
174
+ ///
168
175
func dateByAdding( _ count: Int , _ component: Calendar . Component ) -> DateInRegion {
169
176
DateInRegion ( self , region: SwiftDate . defaultRegion) . dateByAdding ( count, component)
170
177
}
@@ -203,33 +210,43 @@ public extension Date {
203
210
}
204
211
205
212
/// Return the dates for a specific weekday inside a specified date range.
206
- /// NOTE: Values are returned in order.
207
213
///
214
+ /// - note: Values are returned in order.
215
+ ///
208
216
/// - Parameters:
209
217
/// - weekday: weekday target.
210
218
/// - startDate: from date of the range.
211
219
/// - endDate: to date of the range.
212
220
/// - region: region target, omit to use `SwiftDate.defaultRegion`
213
221
/// - Returns: Ordered list of the dates for given weekday in passed range.
214
- static func datesForWeekday( _ weekday: WeekDay , from startDate: Date , to endDate: Date ,
215
- region: Region = SwiftDate . defaultRegion) -> [ Date ] {
222
+ ///
223
+ static func datesForWeekday(
224
+ _ weekday: WeekDay ,
225
+ from startDate: Date ,
226
+ to endDate: Date ,
227
+ region: Region = SwiftDate . defaultRegion
228
+ ) -> [ Date ] {
216
229
let fromDate = DateInRegion ( startDate, region: region)
217
230
let toDate = DateInRegion ( endDate, region: region)
218
231
return DateInRegion . datesForWeekday ( weekday, from: fromDate, to: toDate, region: region) . map { $0. date }
219
232
}
220
233
221
- /// Returns the date at the given week number and week day preserving smaller components (hour, minute, seconds)
234
+ /// Returns the date at the given week number and week day preserving smaller
235
+ /// components (hour, minute, seconds)
222
236
///
223
- /// For example: to get the third friday of next month
224
- /// let today = DateInRegion()
225
- /// let result = today.dateAt(weekdayOrdinal: 3, weekday: .friday, monthNumber: today.month + 1)
237
+ /// For example, to get the third friday of next month:
238
+ /// ```swift
239
+ /// let today = DateInRegion()
240
+ /// let result = today.dateAt(weekdayOrdinal: 3, weekday: .friday, monthNumber: today.month + 1)
241
+ ///```
226
242
///
227
243
/// - Parameters:
228
244
/// - weekdayOrdinal: the week number (by set position in a recurrence rule)
229
245
/// - weekday: WeekDay
230
246
/// - monthNumber: a number from 1 to 12 representing the month, optional parameter
231
247
/// - yearNumber: a number representing the year, optional parameter
232
248
/// - Returns: new date created with the given parameters
249
+ ///
233
250
func dateAt( weekdayOrdinal: Int , weekday: WeekDay , monthNumber: Int ? = nil ,
234
251
yearNumber: Int ? = nil ) -> Date {
235
252
let date = DateInRegion ( self , region: region)
@@ -240,8 +257,9 @@ public extension Date {
240
257
///
241
258
/// - Parameters:
242
259
/// - weekday: weekday to get.
243
- /// - region: region target, omit to use `SwiftDate. defaultRegion`
260
+ /// - region: region target, omit to use `` SwiftDate/SwiftDate/ defaultRegion` `
244
261
/// - Returns: `Date`
262
+ ///
245
263
func nextWeekday( _ weekday: WeekDay , region: Region = SwiftDate . defaultRegion) -> Date {
246
264
let date = DateInRegion ( self , region: region)
247
265
return date. nextWeekday ( weekday) . date
0 commit comments