108 lines
3.6 KiB
Swift
108 lines
3.6 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
|
|
}
|
|
}
|
|
|
|
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.range(of: .day, in: .month, for: self)!.count
|
|
}
|
|
|
|
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() -> String {
|
|
let dateFormatter = DateFormatter()
|
|
dateFormatter.locale = Locale.current
|
|
dateFormatter.setLocalizedDateFormatFromTemplate("MMMM")
|
|
return dateFormatter.string(from: self)
|
|
}
|
|
|
|
func getDay() -> String {
|
|
if #available(iOS 15.0, *) {
|
|
return formatted(.dateTime.day())
|
|
} else {
|
|
// Fallback on earlier versions
|
|
let dateFormatter: DateFormatter = DateFormatter()
|
|
dateFormatter.dateFormat = "d"
|
|
let day: String = dateFormatter.string(from: self)
|
|
return day
|
|
}
|
|
}
|
|
}
|