Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Days using Moment.JS

Having a few issues with simply adding a day to a few dates in an Appcelerator project using moment.js

All I want to do, is grab today's date and then display it in the DD format (01) and then get the next 6 days as well.

Here is what I'm trying:

var todayDate = moment();

var day1 = todayDate.format("DD");
var day2 = todayDate.add(1, 'days').format("DD");
var day3 = todayDate.add(2, 'days').format("DD");
var day4 = todayDate.add(3, 'days').format("DD");
var day5 = todayDate.add(4, 'days').format("DD");
var day6 = todayDate.add(5, 'days').format("DD");
var day7 = todayDate.add(6, 'days').format("DD");

But, the output I get is the following:

[INFO] :   31
[INFO] :   01
[INFO] :   03
[INFO] :   06
[INFO] :   10
[INFO] :   15
[INFO] :   21

It should read:

[INFO] :   31
[INFO] :   01
[INFO] :   02
[INFO] :   03
[INFO] :   04
[INFO] :   05
[INFO] :   06

What am I doing wrong?

Simon

like image 860
Simon Hume Avatar asked Mar 31 '15 13:03

Simon Hume


People also ask

How do you add days to moments?

This is a pretty robust function for adding time to an existing moment. To add time, pass the key of what time you want to add, and the amount you want to add. moment(). add(7, 'days');

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

Just import moment in your file and invoke moment(). add(30, 'days'); it will show the current date time plus 30 days. moment().

How do you add hours to a moment object?

To add hours to a parsed moment date with JavaScript, we can use the add method. to add 5 hours to the moment object parsed from myDate . We call add with 5 and 'hours' to add 5 hours. And then we call format with a format string to return the datetime stored in the moment object.

How do you use moment duration?

var duration = moment. duration("09:30"); var str = moment(duration. _data). format("HH:mm");


2 Answers

You add days to the same variable :

say todayDate is 31. First line, you add 1 day to todayDate, so it becomes 01. Then you add 2 days to todayDate (that is now "01") so it becomes 03 etc ...

Do this instead (depending on what you need of course) :

var day1 = moment().format("DD");
var day2 = moment().add(1, 'days').format("DD");
var day3 = moment().add(2, 'days').format("DD");
var day4 = moment().add(3, 'days').format("DD");
var day5 = moment().add(4, 'days').format("DD");
var day6 = moment().add(5, 'days').format("DD");
var day7 = moment().add(6, 'days').format("DD");

or just add 1 every time ;)

var todayDate = moment();

var day1 = todayDate.format("DD");
var day2 = todayDate.add(1, 'days').format("DD");
var day3 = todayDate.add(1, 'days').format("DD");
var day4 = todayDate.add(1, 'days').format("DD");
var day5 = todayDate.add(1, 'days').format("DD");
var day6 = todayDate.add(1, 'days').format("DD");
var day7 = todayDate.add(1, 'days').format("DD");
like image 66
Cyril N. Avatar answered Sep 18 '22 16:09

Cyril N.


You refer to the same variable

You add N days to todayDate, so next add-method will add N days to the already increased value of todays date, which is no longer 'today'

like image 41
Artur Stary Avatar answered Sep 19 '22 16:09

Artur Stary