嗨,我有两个日期选择器。
日期选择器 1,日期选择器 2。
我需要 datepicker1.date 与 datepicker2.date 进行比较
我需要验证这两个日期是否相同。这怎么可能。在此先感谢。我使用了以下一个,但它不起作用
if([datePicker.date isEqualToDate:FromPicker.date])
嗨,我有两个日期选择器。
日期选择器 1,日期选择器 2。
我需要 datepicker1.date 与 datepicker2.date 进行比较
我需要验证这两个日期是否相同。这怎么可能。在此先感谢。我使用了以下一个,但它不起作用
if([datePicker.date isEqualToDate:FromPicker.date])
使用标准比较方法,比较:
使用-compare:
NSDate 的方法:
if([date1 compare:date2] == NSOrderedSame)
{
//They are the sme
}
我认为您想忽略时间部分。通过以下方式做到这一点:
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...
}
我建议使用timeIntervalSince1970
NSDate *date1; // your first date
NSDate *date2; // your second date
if ([date1 timeIntervalSince1970] == [date2 timeIntervalSince1970]) {
//Equal
}