嗨,我有兩個日期選擇器。如何知道兩個日期是相同的
datepicker1,datepicker2。
我需要datepicker1.date與datepicker2.date
我需要驗證兩個日期相同或不相比較。這怎麼可能。謝謝advance.i已經使用了下面的一個,但它不工作
if([datePicker.date isEqualToDate:FromPicker.date])
嗨,我有兩個日期選擇器。如何知道兩個日期是相同的
datepicker1,datepicker2。
我需要datepicker1.date與datepicker2.date
我需要驗證兩個日期相同或不相比較。這怎麼可能。謝謝advance.i已經使用了下面的一個,但它不工作
if([datePicker.date isEqualToDate:FromPicker.date])
我想你想忽略時間組件。按照以下方式做:
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *components1 = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:YOUR_FIRST_DATE];
NSDateComponents *components2 = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:YOUR_SECOND_DATE];
if ([components1 year] == [components2 year] &&
[components1 month] == [components2 month] &&
[components1 day] == [components2 day])
{
//dates are same...
}
else
{
//dates are different...
}
這很耗時....接下來是一個合適的解決方案..... –
使用標準的比較方法,compare:
使用-compare:
方法的NSDate的:
if([date1 compare:date2] == NSOrderedSame)
{
//They are the sme
}
我會建議使用timeIntervalSince1970
NSDate *date1; // your first date
NSDate *date2; // your second date
if ([date1 timeIntervalSince1970] == [date2 timeIntervalSince1970]) {
//Equal
}
你可以看看答案[這裏](http://stackoverflow.com/a/7736757/443292)。在發佈此類問題之前,我會建議您進行研究。 – Shri