Created
July 22, 2019 14:49
-
-
Save nizzam/c8eac6d5bf0298212feef42d032ffc26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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