如何从 X 中获取下一个 7 天并在 JS 中格式化

How to get next seven days from X and format in JS(如何从 X 中获取下一个 7 天并在 JS 中格式化)
本文介绍了如何从 X 中获取下一个 7 天并在 JS 中格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想打印这样的东西(7 天日历),但可以从我想要的任何日期开始.

I want to print something like this (a 7-day calendar) but with the ability to start from any date I want.

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 日开始的接下来的 7 天.不知道如何处理.

So for example I want to show next seven days from 22 of February. Have no idea how to handle this.

推荐答案

这似乎是你要找的:

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"]

这是一个工作小提琴.

这篇关于如何从 X 中获取下一个 7 天并在 JS 中格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)