我有两个变量,即
date1 = Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
date2 = Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
当我比较我得到的两个日期时date2
更大,我需要的是正确的。但我不想查看我所拥有的两个日期的时间部分。如何从这两个日期单独获取日期部分并进行比较?
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
if(d>=today){ //I need to check the date parts alone.
alert(d is greater than or equal to current date);
}
答案 0 :(得分:57)
尝试使用Date.setHours
清除时间:
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
示例代码:
var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value);
d.setHours(0, 0, 0, 0);
if(d >= today){
alert(d is greater than or equal to current date);
}
答案 1 :(得分:7)
最好的方法是修改已接受的答案if
声明,如下所示
if(d.setHours(0,0,0,0) >= today.setHours(0,0,0,0))
通过这种方式,您可以轻松检查相等性,因为setHours()
的返回类型是整数。
答案 2 :(得分:6)
尝试:
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
var d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
var todayDateOnly = new Date(today.getFullYear(),today.getMonth(),today.getDate()); //This will write a Date with time set to 00:00:00 so you kind of have date only
var dDateOnly = new Date(d.getFullYear(),d.getMonth(),d.getDate());
if(dDateOnly>=todayDateOnly){
alert(d is greater than or equal to current date);
}
答案 3 :(得分:0)
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;