admin管理员组文章数量:1389834
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.
3 Answers
Reset to default 7From 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".
本文标签: javascriptMomentjs shows evaluate one day lessStack Overflow
版权声明:本文标题:javascript - Momentjs shows evaluate one day less - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744580734a2613904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论