Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does moment.js subtract method not works?

I am trying to subtract 7 days to a given date using moment.js

var date ="2015-10-19";
var now =  moment(date);
var oneWeekAgo = moment(date).subtract(7,'days');

When I check values of now and oneWeekAgo have the same content both.

Moment {_isAMomentObject: true, _i: "2015-10-19", _f: "YYYY-MM-DD ", _isUTC: false, _pf: Object…}

However, If I subtract 7 days to current time it works.

var oneWeekAgo = moment().subtract(7,'days');

I've noticed that instanciate a moment without arguments change his structure, storing date on _d attribute instead _i.

Moment {_isAMomentObject: true, _isUTC: false, _pf: Object, _locale: Locale, _d: Tue Oct 13 2015 13:34:50 GMT+0200 (Hora de verano romance)}

Why it happens? and how can I solve it? Thanx.

like image 810
Sapikelio Avatar asked Oct 20 '15 11:10

Sapikelio


People also ask

How do you subtract moments?

MomentJS - Subtract Method //chaining subtract method var changeddate = moment(). subtract(5, 'days'). subtract(2, 'months'); // subtract object method var changeddate1 = moment(). subtract({ days: 5, months: 2 }); //using duration in subract method var duration = moment.

How do you subtract two times using a moment?

add(1, 'day'); // calculate the duration var d = moment. duration(end. diff(start)); // subtract the lunch break d. subtract(30, 'minutes'); // format a string result var s = moment.

Is moment JS still used?

Moment.js has been successfully used in millions of projects, and we are happy to have contributed to making date and time better on the web. As of September 2020, Moment gets over 12 million downloads per week!

What should I use instead of moment JS?

There are several libraries out there that can potentially replace Moment in your app. The creators of Moment recommend looking into Luxon, Day. js, date-fns, js-Joda, or even replacing Moment with native JS.

How do you subtract time in moment JS?

MomentJS - Subtract. Just like the add method, subtract allows to subtract days, months, hours, minutes, seconds etc., from a given date. Syntax. moment().subtract(Number, String); moment().subtract(Duration); moment().subtract(Object);

Is moment mutable in moment JS?

The moment object in Moment.js is mutable. This means that operations like add, subtract, or set change the original moment object. As you can see, adding one week mutated a.

Why can't I add or subtract days in moment's API?

Due to daylight saving time, one day may not equal 24 hours: Due to leap years, one year may not equal 365 days: Because of the variability of duration in day math, Moment's API does not officially support adding or subtracting decimal values for days and larger.

How to manipulate date and time in momentjs?

There are various methods to manipulate Date and Time on the moment object. add, subtract, startoftime, endoftime, local, utc, utcoffset etc., are the methods available which gives details required on date/time in MomentJS. Get/Set allows to read and set the units in the date.


1 Answers

Private variables of momentjs are not that simple.

_i is just the string you use to instanciate the momentjs object. It is not the current value of the date.

var date = "2015-10-19";
var now = moment(date);
var oneWeekAgo = moment(date).subtract(7, 'days');

// 2015 10 19
console.log(now.format('YYYY MM DD'));

// 2015 10 12
console.log(oneWeekAgo.format('YYYY MM DD'));
like image 178
Magus Avatar answered Sep 17 '22 20:09

Magus