admin管理员组

文章数量:1193728

Hi this is incredibly simple i cant believe im asking this.

I have tried the following:

Moment.now()
Date.now()
new Date().valueOf()

And various other tricks found in other stack overflow questions

They all give me the time in microseconds.

e.g

1543409290654

which is 09/10/50878 @ 10:57am (UTC)

Im aware i could divide by 1000 but surely there is an api in javascript to get the unix timestamp in milliseconds.

Ive have seen this in chrome and react native

EDIT:

So i realise my stupidity i was putting the unix timestamp into a website that renders it as an ISO date but it was expecting seconds which is why i thought my dates were coming in as milliseconds

Hi this is incredibly simple i cant believe im asking this.

I have tried the following:

Moment.now()
Date.now()
new Date().valueOf()

And various other tricks found in other stack overflow questions

They all give me the time in microseconds.

e.g

1543409290654

which is 09/10/50878 @ 10:57am (UTC)

Im aware i could divide by 1000 but surely there is an api in javascript to get the unix timestamp in milliseconds.

Ive have seen this in chrome and react native

EDIT:

So i realise my stupidity i was putting the unix timestamp into a website that renders it as an ISO date but it was expecting seconds which is why i thought my dates were coming in as milliseconds

Share Improve this question edited Nov 28, 2018 at 13:14 Luke De Feo asked Nov 28, 2018 at 12:52 Luke De FeoLuke De Feo 2,1654 gold badges24 silver badges41 bronze badges 7
  • 1 What is the desired output you are looking for? and what are you actually getting? – Ahmad Commented Nov 28, 2018 at 12:56
  • 1 Possible duplicate of Getting current unixtimestamp using Moment.js – Hassan Imam Commented Nov 28, 2018 at 12:57
  • 2 Im aware i could divide by 1000 There is your answer, are you having trouble dividing by 1000?. – Keith Commented Nov 28, 2018 at 12:58
  • console.log(Date.now()) returns 1543409203060 this is not a unix millisconds timestamp. its microseconds – Luke De Feo Commented Nov 28, 2018 at 13:02
  • ITS NOT working, please read my question I think everyone appears to have read the question, but you seem to be ignoring them. Your still not dividing by 1000, and wonder why it's wrong. (new Date().getTime() / 1000 | 0) ps. Avoid ITS NOT working, it's not helpful. – Keith Commented Nov 28, 2018 at 13:06
 |  Show 2 more comments

2 Answers 2

Reset to default 16

Date.now() returns the number of milliseconds since midnight January 1, 1970

There is no native javascript to format time into unix-timestamp.

I found this useful

var today = Math.round((new Date()).getTime() / 1000);

console.log(today);

本文标签: react nativeGet current unix time in milliseconds in javascriptStack Overflow