vds_ios/VDS/Components/Calendar/Date+Extension.swift
Matt Bruce 6f17c1b0da refactored to use new calendar
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-05-14 11:23:00 -05:00

107 lines
3.5 KiB
Swift

//
// Date+Extension.swift
// VDS
//
// Created by Kanamarlapudi, Vasavi on 26/04/24.
//
import Foundation
public extension Date {
static var firstDayOfWeek = Calendar.current.firstWeekday
/// Capitalizes the first letter of the day of the week
static var capitalizedFirstLettersOfWeekdays: [String] {
let calendar = Calendar.current
let weekdays = calendar.shortWeekdaySymbols
return weekdays.map { weekday in
guard let firstLetter = weekday.first else { return "" }
return String(firstLetter).capitalized
}
}
/// Returns all month names
static var fullMonthNames: [String] {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
return (1...12).compactMap { month in
dateFormatter.setLocalizedDateFormatFromTemplate("MMMM")
let date = Calendar.current.date(from: DateComponents(year: 2000, month: month, day: 1))
return date.map { dateFormatter.string(from: $0) }
}
}
var startOfMonth: Date {
Calendar.current.dateInterval(of: .month, for: self)!.start
}
var endOfMonth: Date {
let lastDay = Calendar.current.dateInterval(of: .month, for: self)!.end
return Calendar.current.date(byAdding: .day, value: -1, to: lastDay)!
}
/// Get the number of days of the month
var numberOfDaysInMonth: Int {
Calendar.current.component(.day, from: endOfMonth)
}
var firstWeekDayBeforeStart: Date {
let startOfMonthWeekday = Calendar.current.component(.weekday, from: startOfMonth)
var numberFromPreviousMonth = startOfMonthWeekday - Self.firstDayOfWeek
if numberFromPreviousMonth < 0 {
numberFromPreviousMonth += 7 // Adjust to a 0-6 range if negative
}
return Calendar.current.date(byAdding: .day, value: -numberFromPreviousMonth, to: startOfMonth)!
}
/// Get the days of the month to display
var calendarDisplayDays: [Date] {
var days: [Date] = []
// Start with days from the previous month to fill the grid
let firstDisplayDay = firstWeekDayBeforeStart
var day = firstDisplayDay
while day < startOfMonth {
days.append(day)
day = Calendar.current.date(byAdding: .day, value: 1, to: day)!
}
// Add days of the current month
for dayOffset in 0..<numberOfDaysInMonth {
let newDay = Calendar.current.date(byAdding: .day, value: dayOffset, to: startOfMonth)
days.append(newDay!)
}
return days
}
/// Returns the year value of the given date
var yearInt: Int {
Calendar.current.component(.year, from: self)
}
/// Returns the month value of the given date
var monthInt: Int {
Calendar.current.component(.month, from: self)
}
/// Returns the day value of the given date
var dayInt: Int {
Calendar.current.component(.day, from: self)
}
/// Check if the date falls between the given dates
func isBetweeen(date date1: Date, andDate date2: Date) -> Bool {
let from = Calendar.current.date(byAdding: .day, value: -1, to: date1)!
let to = Calendar.current.date(byAdding: .day, value: 1, to: date2)!
return from.compare(self) == self.compare(to)
}
/// Returns the month name of the given date
func getMonthName(date: Date) -> String {
let names = Date.fullMonthNames
return names[date.monthInt - 1]
}
}