-2

Consider code below,

const appointments = [

  {
    // id: 0,
  title: 'Watercolor Landscape',
  users_id: 2,
    startDate: new Date(1584576000000 * 1000),   // here is timestamps in milliseconds 
    endDate:  new Date(1584662400000* 1000),       // here is timestamps in milliseconds
    ownerId: 1,
  },
];

but the output of startDate was wrong, :

enter image description here

It should be:

startDate: Thursday, March 19, 2020 8:00:00 AM GMT+08:00

endDate: Friday, March 20, 2020 8:00:00 AM GMT+08:00

Community
  • 1
  • 1
koko ka
  • 107
  • 4
  • 17

4 Answers4

2

Please simply use: new Date(your_timestamps)
So in your code: startDate: new Date(1584576000000)

Read more about Date object in Javascript here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

1

unix timestamp is already in milliseconds, Date.now() gets timestamp in milliseconds!

to get unix timestamp in seconds you need to divide it in 1000

Math.floor(Date.now() / 1000)
amdev
  • 6,703
  • 6
  • 42
  • 64
1

To get a timestamp in milliseconds in JS you can do:

var date = new Date();
var timestamp = date.getTime();

If you don't intend to support IE8 or previous versions you can use:

Date.now();

NOTE: A timestamp is the number of milliseconds that have passed since January 1, 1970.

Read more here.

Ludolfyn
  • 1,806
  • 14
  • 20
0

It seems to me you just should remove * 1000 and everything will work fine:

console.log(new Date(1584576000000))
// Thu Mar 19 2020 02:00:00 GMT+0200

console.log(new Date(1584662400000))
// Fri Mar 20 2020 02:00:00 GMT+0200
Mark Minerov
  • 309
  • 1
  • 8