44 lines
1.1 KiB
Swift
44 lines
1.1 KiB
Swift
//
|
|
// Date+Extensions.swift
|
|
// Schedule ICTIS
|
|
//
|
|
// Created by G412 on 14.11.2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
|
|
extension Date {
|
|
func format(_ format: String, locale: Locale = Locale(identifier: "ru_RU")) -> String {
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = format
|
|
formatter.locale = locale
|
|
return formatter.string(from: self)
|
|
}
|
|
|
|
func fetchWeek(_ date: Date = .init()) -> [WeekDay] {
|
|
let calendar = Calendar.current
|
|
let startOfDate = calendar.startOfDay(for: date)
|
|
|
|
var week: [WeekDay] = []
|
|
let weekForDate = calendar.dateInterval(of: .weekOfMonth, for: startOfDate)
|
|
|
|
guard let startOfWeek = weekForDate?.start else {
|
|
return []
|
|
}
|
|
|
|
(0..<7).forEach { index in
|
|
if let weekDay = calendar.date(byAdding: .day, value: index, to: startOfWeek) {
|
|
week.append(.init(date: weekDay))
|
|
}
|
|
}
|
|
|
|
return week
|
|
}
|
|
|
|
struct WeekDay: Identifiable {
|
|
var id: UUID = .init()
|
|
var date: Date
|
|
}
|
|
}
|