Skip to content

Commit

Permalink
Add startOfMonth and startOfYear methods to GregorianDay
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Mar 26, 2024
1 parent 31b24a7 commit 1de026a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Sources/HandySwift/Types/GregorianDay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ public struct GregorianDay {
return components.date!
}

/// Returns the start of the month represented by the date.
///
/// - Parameter timeZone: The time zone for which to calculate the start of the month. Defaults to the users current timezone.
/// - Returns: A `Date` representing the start of the month.
///
/// Example:
/// ```swift
/// let startOfThisMonth = GregorianDay.today.startOfMonth()
/// ```
public func startOfMonth(timeZone: TimeZone = .current) -> Date {
let components = DateComponents(
calendar: Calendar(identifier: .gregorian),
timeZone: timeZone,
year: self.year,
month: self.month,
day: 1
)
return components.date!
}

/// Returns the start of the year represented by the date.
///
/// - Parameter timeZone: The time zone for which to calculate the start of the year. Defaults to the users current timezone.
/// - Returns: A `Date` representing the start of the year.
///
/// Example:
/// ```swift
/// let startOfThisYear = GregorianDay.today.startOfYear()
/// ```
public func startOfYear(timeZone: TimeZone = .current) -> Date {
let components = DateComponents(
calendar: Calendar(identifier: .gregorian),
timeZone: timeZone,
year: self.year,
month: 1,
day: 1
)
return components.date!
}

/// Returns the middle of the day represented by the date.
///
/// - Parameter timeZone: The time zone for which to calculate the middle of the day. Defaults to UTC.
Expand Down

0 comments on commit 1de026a

Please sign in to comment.