最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Momentjs shows evaluate one day less - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question asked Jan 31, 2016 at 18:42 Kunal ShewaleKunal Shewale 6966 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

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]);
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.

Try using

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

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

发布评论

评论列表(0)

  1. 暂无评论