javascript - 如何在条件语句中使用 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())
推荐阅读
- flutter - 在 SingleChildScrollView 中实现嵌套的 ListView
- reactjs - 有两个父母的组件孩子,
- clang-format - 铿锵格式标头包括后卫
- node.js - Cheerio 网页抓取 ul > li 属性
- javascript - 无法在套接字 io 上广播多条消息
- apache-spark - Spark SQL 查询问题 - 带有子查询的 SQL 似乎无法检索记录
- c# - 无法创建随机值
- php - Woocommerce 无法更改订单表列的内容
- reactjs - 使用 AWS Lambda 函数在无服务器应用程序上本地测试 Auth0
- mongodb - 如何在mongodb中关联子文档?