Calendar

struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxing

The NSCalendar extension for getting the first/last day.

  • Returns the first day of a given calendar unit (year, quarter, month or week) that contains a given date. For example, first day of the month for June 15, 2016 will be June 1, 2016.

    Declaration

    Swift

    public func firstDay(of unit: Calendar.Component, for date: Date) -> Date

    Parameters

    unit

    The calendar unit to use for the first day calculation.

    date

    The date for which to perform the calculation.

    Return Value

    The first day of the calendar unit of the given date.

  • Returns the last day of a given calendar unit (year, quarter, month or week) that contains a given date. For example, last day of the week for June 15, 2016 will be June 19, 2016.

    Declaration

    Swift

    public func lastDay(of unit: Calendar.Component, for date: Date) -> Date

    Parameters

    unit

    The calendar unit to use for the last day calculation.

    date

    The date for which to perform the calculation.

    Return Value

    The last day of the calendar unit of the given date.

  • Returns an array of dates in the interval with the specified step value. For example, from June 15, 2016 to June 17, 2016 with step 1 day will be June 15, 2016, June 16, 2016 and June 17, 2016.

    Declaration

    Swift

    public func datesBetween(_ startDate: Date, and finishDate: Date, withStep unit: Calendar.Component = .day, value: Int = 1) -> [Date]

    Parameters

    startDate

    The start date of the interval.

    finishDate

    The finish date of the interval.

    unit

    The step unit for the interval. The default value is day.

    value

    The value for the given step unit. The default value is 1.

    Return Value

    An array of dates.