Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的负数if-else条件检查_Javascript - Fatal编程技术网

javascript中的负数if-else条件检查

javascript中的负数if-else条件检查,javascript,Javascript,对于正数,if条件工作正常,但其不执行负数条件可能是错误的原因 //"date is taken as mm/dd/yyyy in input box" var startDateString = document.getElementById('startDate').value; var dateToday = new Date(); alert(dateToday); var myNewDate = process(startDateString); alert(myNewDate);

对于正数,if条件工作正常,但其不执行负数条件可能是错误的原因

//"date is taken as mm/dd/yyyy in input box"
var startDateString = document.getElementById('startDate').value;
var dateToday = new Date();
alert(dateToday);

var myNewDate = process(startDateString);
alert(myNewDate);

var diff = myNewDate - dateToday;
alert(diff);

alert(typeof(diff));

if(diff >= 0){
    alert("selected date is greater than today");
} else {
    alert("selected date is smaller than today");
}
处理功能

function process(date){
    var parts = date.split("/");
    return new Date(parts[2], parts[0] - 1, parts[1]);
}
更改此项:

if(diff >= 0){
  {
   alert("selected date is greater than today");
   } else {
   alert("selected date is smaller than today"); }
例如:

if(diff >= 0){
     alert("selected date is greater than today");
 } else {
   alert("selected date is smaller than today"); 
}

为便于日期比较,请转换为历元并进行比较:

   start = startDate.getTime();
   selected = selectedDate.getTime();
   if(selected >= start) {
     // start date was before selected (or dates are equal)
   } else {
     // selected date was before start
   }

startDateString里面是什么?diff的值和diff的类型是什么?以2012年11月2日的格式说一个日期。我得到的差异是一个数字。。如果日期越大,则为正数,否则负数看起来好像在if语句后有一个过多的括号。。。