最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Converting Year, Month, Day to momentjs Unix timestamp - Stack Overflow

matteradmin9PV0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

Firstly, .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
Post a comment

comment list (0)

  1. No comments so far