gpt4 book ai didi

javascript - 如何递增和递减周?

转载 作者:行者123 更新时间:2023-11-30 05:44:42 25 4
gpt4 key购买 nike

我有递增和递减周的按钮。我必须显示本周的开始日期和结束日期。当我单击增量按钮时,必须显示下周的开始日期和结束日期。如果我再次单击下一周的开始日期和结束日期,则必须显示该日期。减量按钮也是如此。

var i = 0;
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
$('#btnPrevWeek,#btnNextWeek').click(function () {

if ($(this).is('#btnNext')) {
first = first + 7;
last = last + 7;

var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();

var startDatePieces = firstday.split(/[\s,]+/);
var endDatePieces = lastday.split(/[\s,]+/);

var startDate = startDatePieces[2] + " " + startDatePieces[1] + " " + startDatePieces[3];
var endDate = endDatePieces[2] + " " + endDatePieces[1] + " " + endDatePieces[3];
$('#lblWeekStartDate').html(startDate);
$('#lblWeekEndDate').html(endDate);
}
else {
first = first - 7;
last = last - 7;

var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();

var startDatePieces = firstday.split(/[\s,]+/);
var endDatePieces = lastday.split(/[\s,]+/);

var startDate = startDatePieces[2] + " " + startDatePieces[1] + " " + startDatePieces[3];
var endDate = endDatePieces[2] + " " + endDatePieces[1] + " " + endDatePieces[3];
$('#lblWeekStartDate').html(startDate);
$('#lblWeekEndDate').html(endDate);
}
})

我将以这种格式显示日期 - 2013 年 9 月 15 日-2013 年 9 月 21 日

当月代码运行良好,之后就无法正常运行。请帮我解决这个问题。

最佳答案

只需使用日期值:

http://jsfiddle.net/k6jfR/

var week = 7 * 24 * 60 * 60 * 1000;

var curr = new Date();

var first = new Date();
first.setDate(curr.getDate() - curr.getDay());

var last = new Date();
last.setDate(first.getDate() + 6);

var parseDate = function(d){
var datePieces = d.toUTCString().split(/[\s,]+/);
return datePieces[2] + " " + datePieces[1] + " " + datePieces[3];
}
var printDates = function(){
$('#lblWeekStartDate').html(parseDate(first));
$('#lblWeekEndDate').html(parseDate(last));
}

printDates();

$('#btnNextWeek').click(function () {
first = new Date(first.valueOf() + week);
last = new Date(last.valueOf() + week);
printDates();
});
$('#btnPrevWeek').click(function () {
first = new Date(first.valueOf() - week);
last = new Date(last.valueOf() - week);
printDates();
});

关于javascript - 如何递增和递减周?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18658220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com