Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get next seven days from X and format in JS

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

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

like image 875
z-x Avatar asked Apr 05 '12 16:04

z-x


People also ask

How do I get next day in JavaScript?

Using MomentJS: var today = moment(); var tomorrow = moment(today). add(1, 'days');

How do I format a date in JavaScript?

The toDateString() Method in JavaScript First three letters of the week day name. First three letters of the month name. Two digit day of the month, padded on the left a zero if necessary. Four digit year (at least), padded on the left with zeros if necessary.

How do I get current week days in JavaScript?

The getDay() method returns the day of the week (0 to 6) of a date.

How do you add 30 days to a date in typescript?

var future = new Date(); future. setDate(future. getDate() + 30);


2 Answers

This seems to be what you're looking for:

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);​
​

Result (as of today):

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

Here's a working fiddle.

like image 170
James Hill Avatar answered Nov 15 '22 20:11

James Hill


Here is my solution using Moment.js

Next 7 days

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>

Just in case if you need previous 7 days

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>
like image 38
Syed Avatar answered Nov 15 '22 18:11

Syed