Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
Last active December 15, 2020 07:53
Show Gist options
  • Select an option

  • Save SlappyAUS/1baea1c95520c2dc69bc3f28d36bf664 to your computer and use it in GitHub Desktop.

Select an option

Save SlappyAUS/1baea1c95520c2dc69bc3f28d36bf664 to your computer and use it in GitHub Desktop.
DateUtils #swift #date
//
// DateUtil.swift
// DrinkSum
//
// Created by Greg Eales on 5/11/20.
//
import Foundation
struct DateUtil {
static func getModifiedDate(date: Date, currentDate: Date) -> String {
let dateFormatter = DateFormatter()
let difference = Calendar.current.dateComponents([.day], from: currentDate, to: date)
if abs(difference.day!) == 0 || abs(difference.day!) == 1 {
let relativeDateFormatter = RelativeDateTimeFormatter()
relativeDateFormatter.dateTimeStyle = .named
relativeDateFormatter.formattingContext = .beginningOfSentence
let relativePortion = relativeDateFormatter.localizedString(for: date, relativeTo: currentDate)
dateFormatter.setLocalizedDateFormatFromTemplate("MMMMd")
let datePortion = dateFormatter.string(from: date)
return "\(relativePortion) \(difference.day! == 0 ? "" : ", " + datePortion)"
} else {
dateFormatter.setLocalizedDateFormatFromTemplate("MMMMdEEEE")
return dateFormatter.string(from: date)
}
}
static func getTodaysDateString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("MMMMdEEEE")
return dateFormatter.string(from: Date())
}
static func getWeekDayShort(from: Date = Date()) -> String {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("ccccc")
return dateFormatter.string(from: from)
}
static func datePlusDays(forDate: Date, days: Int) -> Date {
return Calendar.current.date(byAdding: DateComponents(day: days), to: forDate)!
}
static func getMonthString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("MMMMyyyy")
return dateFormatter.string(from: Date())
}
}
extension Date {
public func removeTimeStamp() -> Date {
guard let date = Calendar.current.date(
from: Calendar.current.dateComponents([.year, .month, .day], from: self)) else {
fatalError("Failed to strip time from Date object")
}
return date
}
public func beginningOfDay() -> Date {
return self.removeTimeStamp()
}
public func endOfDay() -> Date {
return (Calendar.current.date(
byAdding: .day, value: 1, to: self)?.removeTimeStamp().addingTimeInterval(TimeInterval(-1)))!
}
public var hour: Int {
get {
return Calendar.current.component(.hour, from: self)
}
}
public var minute: Int {
get {
return Calendar.current.component(.minute, from: self)
}
}
public var second: Int {
get {
return Calendar.current.component(.second, from: self)
}
}
public func toString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter.string(from: self)
}
public func toStringWithFormat(format: String = "yyyy-MM-dd'T'HH:mm:ssZ") -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}
func interval(ofComponent comp: Calendar.Component, fromDate date: Date) -> Int {
let currentCalendar = Calendar.current
guard let start = currentCalendar.ordinality(of: comp, in: .era, for: date) else { return 0 }
guard let end = currentCalendar.ordinality(of: comp, in: .era, for: self) else { return 0 }
return end - start
}
public var monthString: String {
get {
return DateUtil.getMonthString()
}
}
private func monthsInCalendar() -> [String] {
let formatter = DateFormatter()
let monthComponents = formatter.shortMonthSymbols!
return monthComponents
}
private func getDayRangeForMonth(monthNumber: Int, yearNumber: Int) {
let dateComponents = DateComponents(
year: yearNumber,
month: monthNumber,
day: 1)
let tmpDate = Calendar.current.date(from: dateComponents)!
let daysInMonth = (Calendar.current.range(of: .day, in: .month, for: tmpDate)?.max())!
return Array(1...daysInMonth)
}
private func getMonthNumberFromName(name: String) -> Int {
return monthsInCalendar().firstIndex(of: month)! + 1
}
private func getMonthNameFromNumber(monthNumber: Int) -> String {
let months = monthsInCalendar()
return months[monthNumber - 1]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment