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

设定

如何new Date()在条件语句中实现javascript对象?我要完成的事情是,当用户使用今天或今天,过去或不到今天两周的日期输入时。条件语句将阻止代码继续使用else

但是输出以某种方式表明满足了条件语句。

这是我的代码:

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")
}
});
})();
格里多

我认为问题在于比较没有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())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章