Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #43 from nodes-vapor/hotfix/start-of-week-vapor1
Browse files Browse the repository at this point in the history
Hotfix/start of week vapor1
  • Loading branch information
steffendsommer authored Nov 6, 2017
2 parents e36bc31 + 5a1af12 commit e166fd4
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions Sources/Date+Sugar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,45 @@ extension Date {
case date = "yyyy-MM-dd"
case ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZ"
}

// MARK: Weekdays
public enum Weekday: Int {
case sunday = 1
case monday
case tuesday
case wednesday
case thursday
case friday
case saturday
}

// MARK: Manipulators

/// Start of week
/// Take you to monday 00:00:00 current week
///
/// - Returns: Date
public func startOfWeek() -> Date {
let calendar = Calendar.current
public func startOfWeek(calendar: Calendar = Calendar(identifier: .gregorian)) -> Date {
var calendar = calendar
calendar.firstWeekday = Weekday.monday.rawValue
var components = calendar.dateComponents([.weekOfYear, .yearForWeekOfYear], from: self.startOfDay())
components.weekday = 2 // Monday
components.weekday = Weekday.monday.rawValue
let startOfWeek = calendar.date(from: components)!
return startOfWeek
}



/// End of week
/// Take you to sunday 23:59:59 current week
///
/// - Returns: Date
public func endOfWeek() -> Date {
let calendar = Calendar.current
public func endOfWeek(calendar: Calendar = Calendar(identifier: .gregorian)) -> Date {
var calendar = calendar
calendar.firstWeekday = Weekday.monday.rawValue
var components = calendar.dateComponents([.weekOfYear, .yearForWeekOfYear], from: self.endOfDay())
components.weekday = 1 // Monday
components.weekday = Weekday.sunday.rawValue
let startOfWeek = calendar.date(from: components)!
return startOfWeek.endOfDay()
}

/// Sub month
///
/// - Returns: Date
Expand Down

0 comments on commit e166fd4

Please sign in to comment.