Closed NikKovIos closed 6 years ago
Here is my solution:
let calendar = Calendar.current
func dateWithComponents(_ components: Set<Calendar.Component>, from date: Date? = nil) -> Date {
let workDate = date ?? now()
guard let resultDate = calendar.date(from: calendar.dateComponents(components, from: workDate)) else {
print("⚠️ DateService >>> Can't fetch date.")
return now()
}
return resultDate
}
func now() -> Date {
return Date()
}
func thisWeekStart() -> Date {
return dateWithComponents([.yearForWeekOfYear, .weekOfYear])
}
func thisMonthStart() -> Date {
return dateWithComponents([.year, .month])
}
func thisYearStart() -> Date {
return dateWithComponents([.year])
}
func lastWeekStart() -> Date {
return thisWeekStart() - 1.weeks
}
func lastWeekEnd() -> Date {
return thisWeekStart() - 1.days
}
func lastMonthStart() -> Date {
return dateWithComponents([.year, .month], from: (now() - 1.months))
}
func lastMonthEnd() -> Date {
return thisMonthStart() - 1.days
}
func lastYearStart() -> Date {
return dateWithComponents([.year], from: (now() - 1.years))
}
func lastYearEnd() -> Date {
return thisYearStart() - 1.days
}
Hello there! I'm using the last Date tools and need to port the old functions to new. Help me please with it.
There is my solution, but i don't think it's correct: