You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
//
|
|
// Date+Extensions.swift
|
|
// LeCountdown
|
|
//
|
|
// Created by Laurent Morvillier on 21/02/2023.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension Date {
|
|
|
|
static let monthYearFormatter = {
|
|
let df = DateFormatter()
|
|
df.dateFormat = "MMMM yyyy"
|
|
return df
|
|
}()
|
|
|
|
static let dayFormatter = {
|
|
let df = DateFormatter()
|
|
df.dateStyle = .short
|
|
df.timeStyle = .none
|
|
return df
|
|
}()
|
|
|
|
static let dateTimeFormatter = {
|
|
let df = DateFormatter()
|
|
df.dateStyle = .short
|
|
df.timeStyle = .short
|
|
return df
|
|
}()
|
|
|
|
var startOfDay: Date {
|
|
return Calendar.current.startOfDay(for: self)
|
|
}
|
|
|
|
func get(_ component: Calendar.Component, calendar: Calendar = Calendar.current) -> Int {
|
|
return calendar.component(component, from: self)
|
|
}
|
|
|
|
var year: Int { self.get(.year) }
|
|
var month: Int { self.get(.month) }
|
|
|
|
var formattedYear: String { return "\(self.year)" }
|
|
var formattedMonth: String { return Date.monthYearFormatter.string(from: self) }
|
|
var formattedDay: String { return Date.dayFormatter.string(from: self) }
|
|
var formattedDateTime: String { return Date.dateTimeFormatter.string(from: self) }
|
|
|
|
}
|
|
|