Date

public extension Date

These functions provide a variety of date manipulation and comparison operations in Swift. From converting dates to strings with specific formats, adding or subtracting months, days, weeks, or years, to calculating the time components between two dates and comparing dates using a custom enum. These functions offer versatile tools for working with dates in different scenarios.

  • Converts the Date object to a string representation using the specified format.

    Example:

      let currentDate = Date()
      let dateString = currentDate.toString(format: "MMMM dd, yyyy")
      print(dateString) // Output: "January 15, 2023"
    

    Declaration

    Swift

    func toString(format: String) -> String

    Parameters

    format

    The format for the resulting date string.

    Return Value

    A string representation of the date in the specified format.

  • Adds a specified number of months to a given date, returning the resulting Date object.

    Example:

      let currentDate = Date()
      if let futureDate = addMonthToDate(monthValue: 3, customDate: currentDate) {
          print(futureDate) // Output: Date representing the current date plus 3 months
      }
    

    Declaration

    Swift

    func addMonthToDate(monthValue: Int) -> Date?

    Parameters

    monthValue

    The number of months to add to the date.

    customDate

    The base Date object to which months will be added.

    Return Value

    A new Date object representing the result of adding the specified months to the input date, or nil if the operation cannot be performed.

  • Adds a specified number of days to the current date, returning the resulting Date object.

    Example:

      let currentDate = Date()
      if let futureDate = addDaysToDate(dayValue: 7) {
          print(futureDate) // Output: Date representing the current date plus 7 days
      }
    

    Declaration

    Swift

    func addDaysToDate(dayValue: Int) -> Date?

    Parameters

    dayValue

    The number of days to add to the current date.

    Return Value

    A new Date object representing the result of adding the specified days to the current date, or nil if the operation cannot be performed.

  • Adds a specified number of weeks to the current date, returning the resulting Date object.

    Example:

      let currentDate = Date()
      if let futureDate = addWeeksToDate(weekValue: 2) {
          print(futureDate) // Output: Date representing the current date plus 2 weeks
      }
    

    Declaration

    Swift

    func addWeeksToDate(weekValue: Int) -> Date?

    Parameters

    weekValue

    The number of weeks to add to the current date.

    Return Value

    A new Date object representing the result of adding the specified weeks to the current date, or nil if the operation cannot be performed.

  • Adds a specified number of years to the current date, returning the resulting Date object.

    Example:

      let currentDate = Date()
      if let futureDate = addYearToDate(yearValue: 5) {
          print(futureDate) // Output: Date representing the current date plus 5 years
      }
    

    Declaration

    Swift

    func addYearToDate(yearValue: Int) -> Date?

    Parameters

    yearValue

    The number of years to add to the current date.

    Return Value

    A new Date object representing the result of adding the specified years to the current date, or nil if the operation cannot be performed.

  • Retrieves the Date object representing the previous year from the current date.

    Example:

      let currentDate = Date()
      if let previousYearDate = getPreviousYear() {
          print(previousYearDate) // Output: Date representing the previous year from the current date
      }
    

    Declaration

    Swift

    func getPreviousYear() -> Date?

    Return Value

    A new Date object representing the date one year before the current date, or nil if the operation cannot be performed.

  • Retrieves the Date object representing the next year from the current date.

    Example:

      let currentDate = Date()
      if let nextYearDate = getNextYear() {
          print(nextYearDate) // Output: Date representing the next year from the current date
      }
    

    Declaration

    Swift

    func getNextYear() -> Date?

    Return Value

    A new Date object representing the date one year after the current date, or nil if the operation cannot be performed.

  • Calculates the time components (years, months, days, hours, minutes, seconds) between two dates.

    Example:

      let currentDate = Date()
      let earlierDate = someEarlierDate // Replace with an actual Date object
      let timeComponents = currentDate.getTimeBetweenDates(previousDate: earlierDate)
      print(timeComponents)
    

    Declaration

    Swift

    func getTimeBetweenDates(endDate: Date) -> (years: Int, months: Int, weeks: Int, days: Int, hours: Int, minutes: Int, seconds: Int)

    Parameters

    previousDate

    The Date object representing the earlier point in time.

    Return Value

    A tuple containing the time components between the two dates. Each component is represented as an optional integer. Returns 0 for any component if the operation cannot be performed.

  • Compares the current date with a given previous date and returns a DateComparison enum.

    Example:

      let currentDate = Date()
      let earlierDate = someEarlierDate // Replace with an actual Date object
      let comparisonResult = getDateComparison(previousDate: earlierDate)
      print(comparisonResult)
    

    Declaration

    Swift

    func getDateComparsion(previousDate: Date) -> DateComparsion

    Parameters

    previousDate

    The Date object representing the earlier point in time.

    Return Value

    A DateComparison enum indicating the relationship between the current date and the provided previous date.