我想打印这样的东西(一个7天的日历),但能够从我想要的任何日期开始。
Monday, 1 January 2011
Tuesday, 2 January 2011
Wednesday, 3 January 2011
Thursday, 4 January 2011
Friday, 5 January 2011
Saturday, 6 January 2011
Sunday, 7 January 2011
例如,我希望从2月22日开始接下来的七天。不知道如何处理这个问题。
答案 0 :(得分:10)
这似乎就是你要找的东西:
function GetDates(startDate, daysToAdd) {
var aryDates = [];
for (var i = 0; i <= daysToAdd; i++) {
var currentDate = new Date();
currentDate.setDate(startDate.getDate() + i);
aryDates.push(DayAsString(currentDate.getDay()) + ", " + currentDate.getDate() + " " + MonthAsString(currentDate.getMonth()) + " " + currentDate.getFullYear());
}
return aryDates;
}
function MonthAsString(monthIndex) {
var d = new Date();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
return month[monthIndex];
}
function DayAsString(dayIndex) {
var weekdays = new Array(7);
weekdays[0] = "Sunday";
weekdays[1] = "Monday";
weekdays[2] = "Tuesday";
weekdays[3] = "Wednesday";
weekdays[4] = "Thursday";
weekdays[5] = "Friday";
weekdays[6] = "Saturday";
return weekdays[dayIndex];
}
var startDate = new Date();
var aryDates = GetDates(startDate, 7);
console.log(aryDates);
结果(截至今天):
["Thursday, 5 April 2012",
"Friday, 6 April 2012",
"Saturday, 7 April 2012",
"Sunday, 8 April 2012",
"Monday, 9 April 2012",
"Tuesday, 10 April 2012",
"Wednesday, 11 April 2012",
"Thursday, 12 April 2012"]
答案 1 :(得分:3)
初始日期:
var startingDay = new Date(year, month, day);
startingDay
整整一周:
var thisDay = new Date();
for(var i=0; i<7; i++) {
thisDay.setDate(startingDay.getDate() + i);
console.log(thisDay.format());
}
格式化功能:
Date.prototype.format = function(){
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return days[this.getDay()]
+", "
+this.getDate()
+" "
+months[this.getMonth()]
+" "
+this.getFullYear();
};
答案 2 :(得分:2)
以下是使用Moment.js
的解决方案接下来的7天
let days = [];
let daysRequired = 7
for (let i = 1; i <= daysRequired; i++) {
days.push( moment().add(i, 'days').format('dddd, Do MMMM YYYY') )
}
console.log(days)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
万一您需要前7天
let days = [];
let daysRequired = 7
for (let i = daysRequired; i >= 1; i--) {
days.push( moment().subtract(i, 'days').format('dddd, Do MMMM YYYY') )
}
console.log(days)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
答案 3 :(得分:1)
var feb22 = new Date(2012, 1, 22);
var feb23 = new Date(feb22.getTime() + 1000*60*60*24);
......等等
答案 4 :(得分:1)
您可以将变量dateString设置为您想要的任何值,并且在循环中您只需增加日期。 然后你会得到日期,但我想以不同的格式。
var dateString = '22 Feb 2012';
var actualDate = new Date(dateString);
var newDate;
for(var i=1; i<=7; i++){
newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+i);
}
答案 5 :(得分:0)
如果从今天开始需要接下来的7个工作日
#include <iostream>
#include <string>
#include <math.h>
#include <iomanip>
bool isPerfect(int n);
using namespace std;
int main() {
long long perfect = 0;
int first = 0;
first = (pow(2, 2 - 1))*(pow(2, 2) - 1);
cout << first << endl;
for (int i = 3, j = 1; j < 5; i += 2) {
if (isPerfect(i)) {
perfect = (pow(2, i - 1)*(pow(2, i) - 1));
cout << perfect << endl;
j++;
}
}
// pause and exit
getchar();
getchar();
return 0;
}
bool isPerfect(int n)
{
if (n < 2) {
return false;
}
else if (n == 2) {
return true;
}
else if (n % 2 == 0) {
return false;
}
else {
bool prime = true;
for (int i = 3; i < n; i += 2) {
if (n%i == 0) {
prime = false;
break;
}
}
return prime;
}
}
&#13;
const isWeekday = (date) => {
return date.weekday()!==0 && date.weekday()!==6
}
const weekdays=[];
let numberOfDaysRequired = 7
let addDaysBy = 1
moment.locale('en')
while(weekdays.length < numberOfDaysRequired)
{
const d = moment().add(addDaysBy, 'days')
if(isWeekday(d))
{
weekdays.push(d.format('dddd, Do MMMM YYYY'))
}
addDaysBy++;
}
console.log(weekdays)
&#13;