javascript中的负数if else条件检查

Negative number if else condition check in javascript

本文关键字:else 条件 检查 if javascript      更新时间:2023-09-29

对于正数,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"); 
}

为了方便日期比较,请转换为epoch并进行比较:

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