我想让javascript返回从上个星期五到星期一的日期。现在,如果当天是在星期五和星期一之间,那么我想在星期五之前回到今天。这是我到目前为止所做的,但我遗漏了一些东西。
function getPrevFriday(){
var friday = 5;
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDay();
var date = currentTime.getDate();
var year = currentTime.getFullYear();
var lastFriday = date - (friday + (7 - day)) % 7+1;
return month + "/" + lastFriday + "/" + year;
};
function getPrevMonday(){
var friday = 5;
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDay();
var date = currentTime.getDate();
var year = currentTime.getFullYear();
var lastFriday = date - (friday + (7 - day)) % 7+4;
return month + "/" + lastFriday + "/" + year;
};
function returnPrevWeekend(){
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDay();
var year = currentTime.getFullYear();
function returnToday(){
return month + "/" + day + "/" + year;
}
$('#from, .startDate').val(getPrevFriday());
if (day < 5 && day > 0) {
$('#to, .endDate').val(getPrevMonday());
$('.date-from').html(getPrevFriday() + ' -<br> ' + getPrevMonday());
}else {
$('#to, .endDate').val(returnToday());
$('.date-from').html(getPrevFriday() + ' -<br> ' + returnToday());
}
};
答案 0 :(得分:1)
类似的东西:
var today = new Date();
var noOfDays = 0;
// Today is friday, saturyda or sunday
if (today.getDay() >= 5) {
noOfDays = today.getDay() - 5;
} else {
// Otherwise
noOfDays = (2 + today.getDay());
}
var days = [];
for(var i = 0; i < 4; i++) {
days.push(new Date(today.getFullYear(), today.getMonth(), today.getDate() - noOfDays + i));
}
console.log(days) // Friday - Monday
答案 1 :(得分:0)
如果周末在几个月之间意识到这个功能正在破裂,我想出了一个类似的解决方案,但是会全面运作:
function returnPrevWeekend(){
var today = new Date();
day = today.getDay();
if (today.getDay() == 0) { //if today is sunday
x = 2; // go back to friday!
}
else if (today.getDay() == 1) { //if today is monday
x = 3; // go back to friday!
y = 0; // don't go back. return today!
}
else if (today.getDay() == 2) {//if today is tuesday
x = 4; // go back to friday!
y = 1; // go back to monday!
}
else if (today.getDay() == 3) {//if today is wednesday
x = 5; // go back to friday!
y = 2; // go back to monday!
}
else if (today.getDay() == 4) {//if today is thursday
x = 6; // go back to friday!
y = 3; // go back to monday!
}
else if (today.getDay() == 5) {//if today is friday
x = 0; // today is friday!
y = 0; // don't go back. return today!
}
else if (today.getDay() == 6) {
x = 1; // go back to friday!
y = 0; // don't go back. return today!
}
d1 = new Date(); // today!
d2 = new Date(); // today!
friday = new Date(d1.setDate(d1.getDate() - x));
lastFriday = friday.getDate();
fridayYear = new Date(friday.setFullYear(friday.getFullYear()));
fridayYear = fridayYear.getFullYear();
fridayMonth = new Date(friday.setMonth(friday.getMonth()));
fridayMonth = friday.getMonth()+1;
monday = new Date(d2.setDate(d2.getDate() - y));
lastMonday = monday.getDate();
mondayYear = new Date(monday.setFullYear(monday.getFullYear()));
mondayYear = mondayYear.getFullYear();
mondayMonth = new Date(monday.setMonth(monday.getMonth()));
mondayMonth = monday.getMonth()+1;
lastFriday = fridayMonth + "/" + lastFriday + "/" + fridayYear;
lastMonday = fridayMonth + "/" + lastMonday + "/" + mondayYear;
$('#from, .startDate').val(lastFriday);
$('#to, .endDate').val(lastMonday);
$('.date-from').html("<span class='abbrDateFrom'>" + lastFriday + "</span>" + '<p>thru</p>'+"<span class='abbrDateTo'>" + lastMonday +"</span>");
};