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.
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.
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.
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!
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.
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);
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.
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.
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.
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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With