我通过点击nextDay和previousDay来增加和减少我的应用程序中的日期,日期正在递增但是一旦它达到月末31,30,29(feb),28(feb)那么它也应该改变月份也是日期。但它继续增加。直到现在我已尝试使用以下代码
var today = new Date();
var days = ["Sunday","Monday","Tuesday",
"Wednesday","Thursday","Friday","Saturday"];
var months = ["January","February","March",
"April","May","June","July","August",
"September","October","November","December"];
var nextDate = 0;
function set_todayDate() {
document.getElementById("todaysDate").innerHTML =
""+days[today.getDay()]+",
"+months[today.getMonth()]+"
"+today.getDate()+"
"+today.getFullYear();
}
对于下一个日期和上一个日期,我正在增加和减少变量
function nextDateMethod(){
nextDate++;
document.getElementById("todaysDate").innerHTML = ""+days[today.getDay()
+dayIncrement]+
", "+months[today.getMonth()]+"
"+(today.getDate()+dayIncrement)+""+today.getFullYear();
}
function prevDate(){
nextDate--;
document.getElementById("todaysDate").
innerHTML = months[today.getMonth()]+",
"+(today.getDate()+nextDate)+" "+today.getFullYear();
}
请有人帮助我
答案 0 :(得分:3)
试试这个:
if (!Date.now) {
Date.now = function() {
return new Date().getTime();
}
}
var theDate = Date.now();
document.getElementById('date').innerText = getTheDate(theDate)
document.getElementById('prev').addEventListener("click", function() {
theDate -= 86400000;
document.getElementById('date').innerText = getTheDate(theDate)
})
document.getElementById('next').addEventListener("click", function() {
theDate += 86400000;
document.getElementById('date').innerText = getTheDate(theDate)
})
function getTheDate(getDate) {
var days = ["Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"
];
var months = ["January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December"
];
var theCDate = new Date(getDate);
return days[theCDate.getDay()] + ', ' + theCDate.getDate() + '-' + months[theCDate.getMonth()] + '-' + theCDate.getFullYear();
}

<div id="date" style="height:100px; width:300px;">
</div>
<button id="prev">
<</button>
<button id="next">></button>
&#13;
答案 1 :(得分:0)
我不明白为什么你的代码应该在日期达到30或31左右时自动更新月份。您正在使用getDate()函数并返回一个数字。然后,您将它增加或减少1.它将简单地表现为var x=y+z
。您将不得不操纵Date
对象或必须手动配置月份和闰年等系统。
我检查了http://www.w3schools.com/jsref/jsref_obj_date.asp处的日期参考对象,看起来你没有像C#那样内置像addDays()那样的功能。所以我想你必须手动实现日历规则。
另外,请提及您声明并初始化nextDate
答案 2 :(得分:0)
这是一个简单的函数,可以在给定日期添加任意天数,诀窍是使用日期对象的setDate方法
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
编辑:由于日期类是可变的,您还可以添加方法以方便将来使用
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + parseInt(days));
return this;
};
答案 3 :(得分:0)
好吧,试试这个(下面是正在运行的样本):
var today = new Date();
date = today.getDate();
month= today.getMonth();
year= today.getFullYear();
month = month+1;
$("#Prev").click(function(){
date = date -1;
if(date<1){
month = month -1;
if(month == 0){
year = year -1;
date = 31;
month = 12;
} else{
if(month == 2){
date = 28
} else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
date = 31
} else{
date = 30
}
}
}
$("#displayDate")[0].innerHTML = date + "/" + month + "/" + year;
});
$("#Next").click(function(){
date = date + 1;
if(date>28 && month == 2){
date = 1;
month = 3;
}
if( date> 30 && (month == 4 || month == 6 || month == 9 || month == 11)){
date = date +1;
month = month +1;
} else if (date> 31){
date =1;
month = month+1;
if(month >12){
year = year +1;
month= 1;
date = 1;
}
}
$("#displayDate")[0].innerHTML = date + "/" + month + "/" + year;
});
$("#displayDate")[0].innerHTML = date + "/" + month + "/" + year;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><button id="Prev">Prev</button><div id="displayDate"></div><button id="Next">Next</button></div>
&#13;
注意: Leap year的条件尚未添加但请告诉我是否是预期的答案我也会添加。
希望这会对你有所帮助:)。
答案 4 :(得分:0)
有一个非常简洁的库,名为moment.js,它处理许多日期操作任务,并提供各种方便的方式来显示日期。网页和文档都做得很好,所以我认为值得看看他们的努力。
这就是增加日期日期所要求的工作如何查看.js:
var new_date_str = moment().add(1, 'days').format("dd, MMMM, D, YYYY");
请参阅此小提琴,了解完整的示例:https://jsfiddle.net/me6pdk8L/2/