Skip to content

Instantly share code, notes, and snippets.

@nizzam
Created July 22, 2019 14:49
Show Gist options
  • Save nizzam/c8eac6d5bf0298212feef42d032ffc26 to your computer and use it in GitHub Desktop.
Save nizzam/c8eac6d5bf0298212feef42d032ffc26 to your computer and use it in GitHub Desktop.
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
df.timeZone = .current
let df2 = DateFormatter()
df2.dateFormat = "yyyy-MM-dd HH:mm:ss"
let stringDate = "2019-09-28"
let customDate = df.date(from: stringDate)
let startDate = df2.date(from: "2019-07-03 14:40:00")
let endDate = df2.date(from: "2019-07-03 14:40:00")
let after1year = Calendar.current.date(byAdding: .year, value: 1, to: Date())
print("AFTER 1 YEAR: ", df.string(from: after1year!))
let today = df.string(from: Date())
print("TODAY: ", today)
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
print("YESTERDAY: ", df.string(from: yesterday!))
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())
print("TOMORROW: ", df.string(from: tomorrow!))
let thisWeek = Calendar.current.dateInterval(of: .weekOfYear, for: customDate!)
print("THIS WEEK: ", df.string(from: thisWeek!.start), df.string(from: thisWeek!.end))
let thisMonth = Calendar.current.dateInterval(of: .month, for: customDate!)
print("THIS MONTH: ", df.string(from: thisMonth!.start), df.string(from: thisMonth!.end - 1))
let lessThan24Hour = Calendar.current.dateComponents([.hour], from: startDate!, to: endDate!)
print("LESS THAN 24 HOUR: ", lessThan24Hour)
print(Date())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment