require('moment')
while the test used require('moment-timezone')
moment().tz('Europe/Oslo').add(1, 'year').month(2).endOf('month')
.weekday(0)
The docs say the following:
Note: if you chain multiple actions to construct a date, you should start from a year, then a month, then a day etc. Otherwise you may get unexpected results, like when day=31 and current month has only 30 days (the same applies to native JavaScript Date manipulation), the returned date will be the 30th of the current month (see month for more details).
Bad: moment().date(day).month(month).year(year)
Good: moment().year(year).month(month).date(day)
Based on that information I would expect:moment().year(2018).month(3).date(31).format()
To print: "2018-04-30T13:20:40-04:00”
But instead it prints: "2018-05-01T13:20:40-04:00”
Why?
.isAfter()
method that I'd like to confirm is unexpected before I make a bug report. It looks like the comparison with day-granularity (moment1.isAfter(moment2, 'day')
) depends upon the locale of moment1
to determine if the day number is equal.
moment(new Date('2018-07-03T03:00:00.000Z')).isAfter(new Date('2018-07-03T00:00:00.000Z'), 'day') //false
moment(new Date('2018-07-03T04:00:00.000Z')).utc().isAfter(new Date('2018-07-03T00:00:00.000Z'), 'day') //false
Hey, I have a problem with .diff()
. following tests are failing
t.is(moment([2019, 9, 30]).diff(moment([2018, 7, 31]), 'months', true), 14); // returns 13.96774193548387
t.is(moment([2019, 2, 28]).diff(moment([2018, 8, 30]), 'months', true), 6); // returns 5.933333333333334
I found something similar in the issue moment/moment#3029. Any clue why this returns the wrong diff? Do I miss something? thanks (using version 2.22.2)
moment(datetime).format("DD"); // result day
moment(datetime).format("MM"); // result month
moment(datetime).format("YYYY"); // result year
moment(datetime).format("HH"); // result hour
moment(datetime).format("mm"); // result minutes
moment(datetime).format("ss"); // result seconds
@Ederagp Thanks for your valuable answer but in your answer you used datetime to convert into desirable format. In my case I've two datetime and I need to know the time difference between two datetime in the format of ( years, months, dates, hours, minutes).
const currentTime // Moment {_isAMomentObject: true, _i: "2018-11-24T10:00:00+05:30", _f: "YYYY-MM-DDTHH:mm:ssZ", _tzm: 330, _isUTC: false, …}
const startTime // Moment {_isAMomentObject: true, _i: Fri Nov 23 2018 13:41:56 GMT+0530 (India Standard Time), _isUTC: false, _pf: {…}, _locale: Locale, …}
Now that I know currentTime and startTime and I need to know the time difference and after digging through the momentJs docs I found that I can use to
like this
const result = currentTime.to(startTime)
but above code return In a day
but what I want is years, months, hours, minutes, seconds
Please help me to understand how to get this desirable format what I want.
moment().diff(Moment | String | Number | Date | Array);
a.diff(b, 'years');
a.diff(b, 'days');