Skip to content

Instantly share code, notes, and snippets.

@dheerajn
Last active September 15, 2021 22:44
Show Gist options
  • Save dheerajn/03bb1ab30625b656c1cd247f8a1821f1 to your computer and use it in GitHub Desktop.
Save dheerajn/03bb1ab30625b656c1cd247f8a1821f1 to your computer and use it in GitHub Desktop.
extension TimeZone {
 public static let localTimeZone: TimeZone = TimeZone(identifier: "Asia/Calcutta")!
}
extension Date {
struct _Formatter {
static let base: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.long
formatter.timeZone = TimeZone.localTimeZone
return formatter
}()
static let concise: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MM/dd/yyyy"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
}()
static let conciseWithoutYear: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MM/dd"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let dayMonthDate: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "EEEE MMMM dd"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let monthDayYear: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MMM dd, yyyy"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let yearMonthDate: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "yyyy-MM-dd"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let hourMinuteDate: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "hh:mm a z"
 return formatter
 }()
static let monthDate: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MMMM dd"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let monthDateWithoutLeadingZero: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MMMM d"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let monthDateNumberFormatWithoutLeadingZero: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "M/d"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let shortMonthDate: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MMM d"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let yearDate: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "yyyy"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let month: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MMMM"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let monthDateYear: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MMM d, yyyy"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let fullMonthDateYear: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MMMM dd, yyyy"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let monthDateYearWithoutLeadingZero: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "MMMM d, yyyy"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
static let yearMonthDatehourMinSec: DateFormatter = {
 let formatter = DateFormatter()
 formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
 formatter.timeZone = TimeZone.localTimeZone
 return formatter
 }()
}
 /// Returns a string representation of this date using a standard app-wide format
public var defaultFormattedString: String {
return _Formatter.base.string(from: self)
}
 /// Returns a concise, number format of this date (ex. 11/11/2016) depending on the locale of the device
public var conciseFormattedString: String {
return _Formatter.concise.string(from: self)
}
 /// Returns a concise, number format of this date (ex. 09/27) depending on the locale of the device
public var conciseWithoutYearFormattedString: String {
return _Formatter.conciseWithoutYear.string(from: self)
}
/// Returns a date in the following format: Wednesday March 19
public var depositStyleFormattedString: String {
 return _Formatter.dayMonthDate.string(from: self)
 }
/// Returns a date in the following format: 10:00 am IST
public var expiryStyleFormattedString: String {
 return _Formatter.hourMinuteDate.string(from: self)
 }
/// Returns a date in the following format: July 10
public var monthDate: String {
 return _Formatter.monthDate.string(from: self)
 }
/// Returns a date in the following format: July 10,2019
public var fullMonthDateYear: String {
 return _Formatter.fullMonthDateYear.string(from: self)
 }
/// Returns a date in the following format: Jul 10
public var shortMonthDate: String {
 return _Formatter.shortMonthDate.string(from: self)
 }
/// Returns a formatted date ie May 1 or July 1
public var monthDateWithoutLeadingZero: String {
 return _Formatter.monthDateWithoutLeadingZero.string(from: self)
 }
/// Returns a date in the following (month/Date)format: 8/9
public var monthDateNumberFormatWithoutLeadingZero: String {
 return _Formatter.monthDateNumberFormatWithoutLeadingZero.string(from: self)
 }
/// Returns a formatted full month date ie September 4, 2019
public var monthDateYearWithoutLeadingZero: String {
 return _Formatter.monthDateYearWithoutLeadingZero.string(from: self)
}
/// Returns a date in the following format: 2019
public var yearDate: String {
 return _Formatter.yearDate.string(from: self)
 }
/// Returns a date in the following format: September 21, 2019
public var monthDateYear: String {
 return _Formatter.monthDateYear.string(from: self)
 }
/// Returns month: February
public var month: String {
 return _Formatter.month.string(from: self)
}
/// Checks if the given date is between fromDate and toDate
///
/// - Parameters:
/// - fromDate: Starting Date
/// - toDate: Ending Date
/// - Returns: Returns true if it falls between 2 dates else false
public func isBetween(_ fromDate: Date, _ toDate: Date) -> Bool {
return (min(fromDate, toDate)...max(fromDate, toDate)).contains(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment