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.
60 lines
1.6 KiB
60 lines
1.6 KiB
//
|
|
// Date+Extensions.swift
|
|
// LeCountdown
|
|
//
|
|
// Created by Laurent Morvillier on 21/02/2023.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension Date: Identifiable {
|
|
|
|
public var id: TimeInterval { return self.timeIntervalSince1970 }
|
|
|
|
}
|
|
|
|
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 = .medium
|
|
return df
|
|
}()
|
|
|
|
var startOfDay: Date {
|
|
return Calendar.current.startOfDay(for: self)
|
|
}
|
|
|
|
var startOfMonth: Date {
|
|
let calendar = Calendar(identifier: .gregorian)
|
|
let components = calendar.dateComponents([.year, .month], from: self)
|
|
return calendar.date(from: components)!
|
|
}
|
|
|
|
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).capitalized }
|
|
var formattedDay: String { return Date.dayFormatter.string(from: self) }
|
|
var formattedDateTime: String { return Date.dateTimeFormatter.string(from: self) }
|
|
|
|
}
|
|
|