标签云

微信群

扫码加入我们

WeChat QR Code

How can I get a timestamp in JavaScript?Something similar to Unix's timestamp, that is, a single number that represents the current time and date. Either as a number or a string.


How to get a 64bit timestamp in JS?

2019年06月25日26分48秒

AlexanderMills Since JavaScript doesn't support 64bit integers, you'll have to get two 32-bit integers. Ask another question.

2019年06月26日26分48秒

Another notable exception is node.js. I came here from Google looking to do the same in node

2019年06月26日26分48秒

Won't Date.now() / 1000 | 0 suffer from the Year 2038 problem? new Date('Jan 1, 2039') / 1000 | 0 == -2117514496

2019年06月25日26分48秒

or even, w/o using Math.floor.. ~~(Date.now() / 1000)

2019年06月25日26分48秒

MilanBabuškov but node.js does support Date.now()

2019年06月25日26分48秒

While +new Data is clever, it isn't readable by other devs. Date.now() is clearer and ensures everyone else knows what's going on.

2019年06月25日26分48秒

This option is a bad idea. It's easily overlooked, looks like a typo, and is in reality relying on a language side-effect. I see bad code.

2019年06月26日26分48秒

Billy As I recall it, I computed the timestamp in the two suggested solutions 1M times each, and calculated the average runtime. I ran it in Firefox and Chrome, with getTime being faster in both browsers. That said, even if it were (marginally) slower I'd choose new Date().getTime(). Luckily for me, the faster solution is already the legible solution!

2019年06月25日26分48秒

Agreed with FabrícioMatté.Unary operator behavior may not be rudimentary, but if you haven't brushed up on it, don't expect to be able to function effectively in a lot of teams.

2019年06月25日26分48秒

Niklaus That's because you're concatenating it to another string. In that case, new Date().toString() is called.

2019年06月25日26分48秒

out of curiosity what is the +operator doing to make it come out like a string?

2019年06月25日26分48秒

PHP should work fine with milliseconds, as it uses them itself with the microtime() function.

2019年06月25日26分48秒

While microtime() is present, most time related functions in php expect the timestamp to be in seconds and not milliseconds.What's more is that microtime() returns a float (if you pass true) where the decimal part is the fractions of a second (accurate to the microsecond), while newDate().getTime() returns an int where it just counts milliseconds since the epoch.For example (php) if you were to call floor(microtime(true)) this would be effectively the same as calling time() which is in seconds and not micro or milliseconds. Dividing by 1000 as above is the easiest solution to this.

2019年06月25日26分48秒

Instead of round, use floor. 700 ms still isn't whole second

2019年06月26日26分48秒

Why the || operator? Is Date.now() not available on all browsers?

2019年06月25日26分48秒

Apparently not, I found the code in modulejs

2019年06月25日26分48秒

Date.now() is from JavaScript 1.5, and is not supported on IE 8.

2019年06月25日26分48秒

Engines which have not been updated to support the Date.now method can work around the absence of it using the following shim:if (!Date.now) { Date.now = function now() { return +(new Date); }; }

2019年06月25日26分48秒

|0 is similar to Math.floor() since it is a bit operation (that does not work with floats). usualy its even faster than Math.floor() since it is not a function call, it is a native javascript operator.

2019年06月25日26分48秒

Date.now() reference: developer.mozilla.org/en-US/docs/JavaScript/Reference/…

2019年06月25日26分48秒

Christophe why do you think that? because of the IE fix? except that part its pretty simple and customizable i think.

2019年06月25日26分48秒

polyfills / shims are not complicated. They are a result of having to support 9 different browser versions and the industry deciding to choose which standards they adopt and which they just make up.

2019年06月26日26分48秒

For reference the most compact you can get is new Date/1e3|0.

2019年06月25日26分48秒

jQuery will also take care of any cross-browser compatibility issues with the different JavaScript APIs.

2019年06月25日26分48秒

VisioN I looked for a way to do timestamps in Node.js. I found a javascript question in stackoverflow and I get an answer in jQuery, which doesn't help. Javascript and jQuery are two things. If we give answers to every javascript framework out there, we end up with a possibly infinite number of answers, which is against the philosophy of Q/A in stackoverflow. Also: i.stack.imgur.com/Ledgt.gif :D

2019年06月25日26分48秒

VisioN Who said javascript needs to be used in a browser?

2019年06月25日26分48秒

PierreArlaud And who said that this question is about NodeJS only?

2019年06月26日26分48秒

KingRider There is no need in parseInt, as the resulting value will always be a Number.

2019年06月25日26分48秒

This equals +new Date.. :)

2019年06月26日26分48秒

just do Date.now()

2019年06月26日26分48秒

Orlando see the other answers, the issue with now() is browser support

2019年06月25日26分48秒

Christophe just do a simple polyfill, developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

2019年06月25日26分48秒

Orlando exactly, don't just do Date.now()

2019年06月25日26分48秒

The OP is asking about an integer timestamp; this is a string representation of the time.

2019年06月25日26分48秒

"similar to Unix's timestamp, that is, a single number that represents the current time"

2019年06月26日26分48秒

I had a code to display time but parts of Australia showed full date, this script helped me break it down and display right. (+1 for that)

2019年06月25日26分48秒

It does not return an integer with the question is asked for!

2019年06月25日26分48秒

AlexVentura with an string dateformat ISO you can instantiate an date object and subtract between them. For example: new Date("2017-05-04T07:11:28.940Z") - new Date("2017-05-04T07:11:14.092Z")

2019年06月26日26分48秒

Date.now() /1000 |0 does the same and has been answered before

2019年06月25日26分48秒

well. i sure edited Math.floor() into it but only to make clear that it has the same effect as |0. i did not want to hurt you or anything. i just wanted to tell you that your answer in some kind of way already exists. by looking at all given answers there are some that notice floor in some way.

2019年06月25日26分48秒

I think importing the entire Lodash library just to use _.now() is a little bit overkill. I would recommend only importing the Lodash functionality you need to use (in this case, _.now() on NPM), but even they have deprecated their package in favour of just using Date.now().

2019年06月25日26分48秒

You don't need the parentheses: new Date().getTime() / 1000

2019年06月25日26分48秒

You don't need them, but they make code more readable (IMHO). Matter of taste I guess.

2019年06月25日26分48秒

You don't even need () after new Date and .getTime() since / will cast the Date object into a number anyway.

2019年06月25日26分48秒

Yeah, don't minify code manually, especially to save 1 byte.

2019年06月25日26分48秒

dchest"Math.floor(new Date().getTime()/1000)".length == 37;"new Date/1E3|0".length == 14;37 -14 = 23 bytes;

2019年06月25日26分48秒

A great way to make your code unreadable...

2019年06月26日26分48秒

Valentin You are minifying/complicating simple code for almost no benefits. I don't see the point to do that. We are not on Code Golf!

2019年06月25日26分48秒

Valentin Please explain me how replacing Math.floor() by |0 is making this code "clear and understandable". Explain me what is the point to save 1 byte by replacing 1000 by 1E3. This is ridiculous. I see bad code.

2019年06月25日26分48秒

b123400 - Here's the Lisp version: (new (chain (-date) (to-i-s-o-string))).

1970年01月01日00分03秒

(Rephrasing my comment) Your code has a problem: it executes Date.now method instead of checking its support first. On older browsres it will cause Date.now is not a function error.

2019年06月26日26分48秒

Perhaps a better alternative would be to use a ternary operator to assert that Date.now actually exists (and is a function), before attempting to invoke it: currentTime = typeof Date.now === "function" ? Date.now() : +new Date().

2019年06月25日26分48秒

I was about to write the new Date() Object .You can console log(new Date()) and then skim through the related methods under the new Date() object/ function

2019年06月25日26分48秒

nice advanced scripts, good jobs!

2019年06月25日26分48秒

Note that this gives the number of seconds (not milliseconds) since UNIX epoch. If you want the milliseconds, use moment().valueOf(). See my answer.

2019年06月25日26分48秒

What is event?

2019年06月26日26分48秒

Do know where event comes from. You need to give a better explanation of the way you resolve it instead of you writing an answer. Please!

2019年06月25日26分48秒

I was about to write the new Date() Object .You can console log(new Date()) and then skim through the related methods under the new Date() object/ function

2019年06月25日26分48秒

Even shorter: new Date().getTime()/1000|0 but its slow and dirty

2019年06月26日26分48秒

I was about to write the new Date() Object .You can console log(new Date()) and then skim through the related methods under the new Date() object/ function

2019年06月25日26分48秒