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-07-02 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-07-02 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
Naming style camel,snake,kebab,pascal cases tutorial example javascript Add padding, leading zero to a string number How to Display JavaScript object with examples How to convert decimal to/from a hexadecimal number in javascript How to convert character to/from keycode in javascript examplesRelated posts