我正在使用bootstrap插件作为datepicker,我写了一个函数来检查它是否是今天使用纯粹的js ..
什么都没发生,我想知道为什么.. datepicker值的格式如dd / mm / yyyy
var datepicker = $('#datepicker').val();
checkToday(datepicker);
function checkToday(datepicker){
datepickerDay = datepicker.slice(0, datepicker.indexOf('/'));
var d = new Date();
var dToday = d.getDate() ;
dToday.toString();
if(datepickerDay == dToday){
return "Today"
alert('Today');
}
else{
return datepicker;
alert(datepicker);
}
}
答案 0 :(得分:0)
不要使用 parseInt 也不能在返回后放任何东西
var datepicker = $('#datepicker').val();
function checkToday(datepicker) {
datepickerDay = datepicker.slice(0, datepicker.indexOf('/'));
var d = new Date();
var dToday = d.getDate();
dToday.toString();
if (datepickerDay == dToday) {
return "Today";
} else {
return datepicker;
}
}
alert(checkToday(datepicker));
答案 1 :(得分:0)
您可以转换日期格式的日期,然后按日期,月份和年份进行比较。
试试这个
function checkToday(datepicker){
var datepickerDay = new Date(datepicker); //creating date object from string 'mm/dd/yyyy'
var d = new Date(); // date object
// now lets comapare date, Month and year to get correct result
if(d.getDate()==datepicker.getDate() && d.getMonth()==datepicker.getMonth() && d.getYear()==datepicker.getYear()){
return "Today"
alert('Today');
}
else{
return datepicker;
alert(datepicker);
}
}
希望这会有所帮助......