admin管理员组文章数量:1304184
I'm trying to get the same value as 1502755200000
using momentjs
console.log(Date.UTC(2017, (8-1), 15));//1502755200000
var newDate = moment();
newDate.set('year', 2017);
newDate.set('month', 7); // April
newDate.set('date', 15);
console.log(newDate.format('X'));//1502818350
However when I try to get the miliseconds I get 1502818350
Any idea how to get the exact same timestamp as above?
Here is the fiddle /
I'm trying to get the same value as 1502755200000
using momentjs
console.log(Date.UTC(2017, (8-1), 15));//1502755200000
var newDate = moment();
newDate.set('year', 2017);
newDate.set('month', 7); // April
newDate.set('date', 15);
console.log(newDate.format('X'));//1502818350
However when I try to get the miliseconds I get 1502818350
Any idea how to get the exact same timestamp as above?
Here is the fiddle https://jsfiddle/cdvzoezb/1/
Share Improve this question edited Sep 20, 2017 at 17:41 Temani Afif 274k28 gold badges364 silver badges484 bronze badges asked Sep 20, 2017 at 17:37 BaconJuiceBaconJuice 3,77915 gold badges59 silver badges90 bronze badges2 Answers
Reset to default 6Firstly, .format('X')
gives you the unix timestamp in seconds, not milliseconds.
To get milliseconds, you must use .format('x')
(lowercase x).
Secondly, when you use moment()
, it gives you a moment date object at your current local time, not UTC time. So when you modify it with .set('date', 15)
etc, you're setting it to 15 April 2017 in your local time. This is why you're getting the vastly different value.
To get a moment date object for current UTC time, use moment.utc()
.
Thirdly, the Date object you created will be at time 00:00:00.000, while the moment object will be for current time. So when you set the year/month/date, time still remains at what it was when you created the object. You need to set the moment object's time to 00:00:00.000.
This can be done with the .startOf('day')
function.
In conclusion:
console.log(Date.UTC(2017, (8-1), 15)); //1502755200000
var newDate = moment.utc();
newDate.set('year', 2017);
newDate.set('month', 7);
newDate.set('date', 15);
newDate.startOf('day');
console.log(newDate.format('x')); //1502755200000
Or, much shorter:
var newDate = moment.utc('2017-07-15 00:00:00.000');
Well you can simply create a moment object out of a Date
instance and then using the utc()
to convert the timestamp to UTC. After that, we can use moments method format()
to get milliseconds using the x
display option like so:
console.log("==============");
console.log(Date.UTC(2017, (8-1), 15));
var base = Date.UTC(2017, (8-1), 15)
var newDate = moment(base);
console.log('a', newDate.utc().format('x')); //1502755200000
本文标签: javascriptConverting YearmonthDay to momentjs Unix timestampStack Overflow
版权声明:本文标题:javascript - Converting Year, Month, Day to momentjs Unix timestamp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741774532a2396946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论