Kunal Shewale
Kunal Shewale

Reputation: 696

Momentjs shows evaluate one day less

I am using Momentjs to evaluate the selected date in react-datetimepicker. But it evaluate one day less than the selected day. Following is the code snippet I am using to evaluate selected date.

var selectedDateArr = ['2016','02','04']
var yr = parseInt(selectedDateArr[0]), mnth = parseInt(selectedDateArr[1] - 1), day = parseInt(selectedDateArr[2]);
var changedDt = moment([yr,mnth,day]);
console.log('Changed Date >>> ',changedDt);

It gives following object as a console result in firefox.

{ _isAMomentObject: true, _i: Date 2016-01-03T18:30:00.000Z, _isUTC: false, _pf: Object, _locale: Object, _d: Date 2016-01-03T18:30:00.000Z }

I have tried to convert the date to UTC but still no luck.

Note: In chrome it shows the correct evaluation but while supplying it as minDate it breaks; again evaluate one day less than selected.

Upvotes: 1

Views: 4751

Answers (3)

Sriman Pathy
Sriman Pathy

Reputation: 259

Try using

var changedDt = moment.tz([yr,mnth,day], timezone).

For example I am in India so my timeZone will be "Asia/Kolkata".

Upvotes: 0

J. Pravin
J. Pravin

Reputation: 1

moment.unix(timestamp).utc().format('MM/DD/YYYY')
moment.utc(date).format('MM/DD/YYYY')

if we pass it will work for me it works.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074666

From the values shown in your question, I'm guessing you're in India, which means your local timezone (IST) is GMT+0530 (five and a half hours ahead of GMT). When you give Moment an array, you're giving it local time values, and any values you don't supply are defaulted to 0 — in your case, since you don't provide any time values, that's midnight. Midnight on Jan 4th in GMT+0530 is 18:30 on Jan 3rd in UTC.

If you want the values treated as UTC, use moment.utc(...):

var changedDt = moment.utc([hr,mnth,day]);

Upvotes: 7

Related Questions