Swift में दो डेट्स की तुलना
इस लेख में हम देखेंगे कि Swift में दो NSDate की तुलना कैसे करें। सबसे पहले हमें दो NSDate बनाने होंगे।
हम इसे सिम्युलेटर के बजाय प्लेग्राउंड में करेंगे।
पहले दो अलग-अलग डेट्स बनाएं:
let dateOne = DateComponents()
dateOne.day = 5
dateOne.month = 6
dateOne.year = 1993
let dateTwo = DateComponents()
dateTwo.day = 4
dateTwo.month = 2
dateTwo.year = 1995
इन डेट कंपोनेंट्स का उपयोग करके हम डेट्स बनाएंगे और फिर उनकी तुलना करेंगे:
let cal = Calendar.current
let firstDate = cal.date(from: dateOne)!
let secondDate = cal.date(from: dateTwo)!
अब उनकी तुलना करने के लिए हम if कंडीशन का उपयोग करेंगे:
if secondDate.compare(firstDate) == .orderedAscending {
print("date 1 is bigger than date 2")
} else {
print("Date 2 is bigger")
}
उपरोक्त कोड को सिम्युलेटर पर चलाने पर आउटपुट:

तुलना करने के तीन अलग-अलग तरीके हैं:
- orderedAscending
- orderedDescending
- orderedSame