简体   繁体   English

如何在条件语句中使用 javascript new Date() 对象?

[英]How do I use javascript new Date() objects in conditional statements?

How do I implement javascript new Date() objects in a conditional statement?如何在条件语句中实现 javascript new Date()对象? What I want to accomplish is when a user uses a date input that is either today, a date that passed, or less than two weeks from today.我想要完成的是当用户使用今天、过去的日期或距今天不到两周的日期输入时。 The conditional statement will prevent the code from continuing using else条件语句将阻止代码继续使用else

but somehow the output shows that the conditional statement is satisfied.但不知何故,输出显示条件语句得到满足。

Here is my code:这是我的代码:

var getDateAndConsent = {
    getGoalDate: new Date($('#achiveG').val()), //"dd-mm-yyyy"
    getYN: $('inputMaintain').val(),
} //getGoalDate: new Date($('#achiveG').val()), this code should get the input date which returns as a string

//nextnextweek
Date.prototype.addDays = function() {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + 21);
    return date;
}//sets the date object for two weeks from now

//yesterday
Date.prototype.minusDays = function() {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() - 1);
    return date;
}//sets the date object for yesterday

//vars to store the converted new Date() objects
var today = new Date();
var date1 = new Date();
var date2 = new Date();

alert(today); //returns Thu Feb 06 2020 19:44:58 GMT+0800 (Philippine Standard Time)
alert(date1.addDays()); //Thu Feb 27 2020 19:44:58 GMT+0800 (Philippine Standard Time)
alert(date2.minusDays()); //Wed Feb 05 2020 19:44:58 GMT+0800 (Philippine Standard Time)
alert(getDateAndConsent.getGoalDate); //Wed Feb 12 2020 08:00:00 GMT+0800 (Philippine Standard Time) this is a user input example only

if (getDateAndConsent.getGoalDate < date1.addDays() || getDateAndConsent.getGoalDate > date2.minusDays() || getDateAndConsent.getGoalDate == today){
    firebase.auth().onAuthStateChanged(function(user){
        if(user){
            this.userId = user.uid; //stores the userid
        }
    firebase.firestore().collection("users").doc(userId).collection("goal").doc(americanDate).set({
        'bgReading': data.inputWeight, 'dateAdded': data.dateAdded, 
        'isActive': data.isActive,
    })
    .then(function(){
        window.alert("Weight goal updated!");
        window.location.href = "diabetesManagement.php"; 
    })
    .catch(function(error){
        console.log("Error updating weight goal: ", error);
        window.alert("Error updating weight goal: " + error);
    });
});
}
else{
window.alert("error date")
}
});
})();

I think the issue rely in comparing dates without getTime()我认为这个问题依赖于比较没有 getTime() 的日期

 var getDateAndConsent = { getGoalDate: function(){ var x = new Date(); x.setDate(x.getDate()+10); return x; }() } //nextnextweek Date.prototype.addDays = function() { var date = new Date(this.valueOf()); date.setDate(date.getDate() + 21); return date; }//sets the date object for two weeks from now //yesterday Date.prototype.minusDays = function() { var date = new Date(this.valueOf()); date.setDate(date.getDate() - 1); return date; }//sets the date object for yesterday //vars to store the converted new Date() objects var today = new Date(); var date1 = new Date(); console.log(today); console.log(date1.addDays()); console.log(getDateAndConsent.getGoalDate); console.log(getDateAndConsent.getGoalDate.getTime() < date1.addDays().getTime())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM