THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorials shows if given date is before current date or today.
The below examples check only Dates, not time
Current date returns using new Date() object. You can create past and future date using date method It accepts
byAdding: tells to add .day or any value: value can be positive or negative to: specify current date.
Dates can be compared using <
operator
Here is an example to check give date is before current date
import Foundation
let date = Date()
let pastDate = Calendar.current.date(byAdding: .day, value: -1, to: date)!
print(date) //2022-07-03 06:13:45 +0000
print(pastDate)//2022-10-30 06:13:45 +0000
if(pastDate<date){
print("pastDate is before current date")
}
Output:
2022-07-03 06:13:45 +0000
2022-07-04 06:13:45 +0000
2022-10-30 06:13:45 +0000
pastDate is before current date
Dates can be compared using > for checking after date.
import Foundation
let date = Date()
let futureDate = Calendar.current.date(byAdding: .day, value: 1, to: date)!
print(date) //2022-07-03 06:13:45 +0000
print(futureDate)//2022-07-04 06:13:45 +0000
if(futureDate>date){
print("futureDate is before current date")
}
Output:
2022-07-03 06:13:45 +0000
2022-07-04 06:13:45 +0000
futureDate is before current date
🧮 Tags
Recent posts
Puppeteer Login Test example How to convert Double to Integer or Integer to double in Dart| Flutter By Example Best ways to fix 504 gateway time out in nodejs Applications Fix for Error No configuration provided for scss Multiple ways to List containers in a Docker with examplesRelated posts