All In One Script



PHP,HTLM,CSS,Jquery,AJAX,Javascript and etc doubts and sample codes

  • Home
  • Javascript
  • PHP
  • CSS
  • SQL/MYSQL

How do you get a timestamp in JavaScript?

by Blogger 4:42:00 AM Date Datetime Javascript Timestamp unix-timestamp

How do you get a timestamp in JavaScript?

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.

Answer:

Short & Snazzy:
+ new Date()
A unary operator like plus triggers the valueOf method in the Date object and it returns the time-stamp (without any alteration).
Details:
On almost all current browsers you can use Date.now() to get the UTC timestamp in milliseconds; a notable exception to this is IE8 and earlier (see compatibility table).
You can easily make a shim for this, though:
if (!Date.now) {
    Date.now = function() { return new Date().getTime(); }
}
To get the timestamp in seconds, you can use:
Math.floor(Date.now() / 1000)
Or alternatively you could use:
Date.now() / 1000 | 0
Which should be slightly faster, but also less readable (also see this answer).
I would recommend using Date.now() (with compatibility shim). It's slightly better because it's shorter & doesn't create a new Date object. However, if you don't want a shim & maximum compatibility, you could use the "old" method to get the timestamp in milliseconds:
new Date().getTime()
Which you can then convert to seconds like this:
Math.round(new Date().getTime()/1000)
And you can also use the valueOf method which we showed above:
new Date().valueOf()



http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript
READ MORE
SHARE :

Search This Blog

Followers

  • Popular
  • Recent
  • Comments
    Insert into … values ( SELECT … FROM … )
    Why does this code using random strings print “hello world”?
    What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
    How to avoid Java code in JSP files?
    How can I transition height: 0; to height: auto; using CSS?
    How to efficiently iterate over each Entry in a Map?
    Event binding on dynamically created elements?
    mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given
    In Java, difference between default, public, protected, and private
    How to remove a property from a JavaScript object?

Instagram

About

Popular Posts

  • Insert into … values ( SELECT … FROM … )
    Insert into … values ( SELECT … FROM … ) I am trying to insert into a table using the input from another table. Although this is entire...
  • Why does this code using random strings print “hello world”?
    Why does this code using random strings print “hello world”? The following print statement would print "hello world". Could a...
  • What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
    What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?  What's the difference between  INNER JOIN ,  LEF...
  • How to avoid Java code in JSP files?
    How to avoid Java code in JSP files? I'm new to Java EE and I know that something like the following three lines <%= x + 1 %...
  • How can I transition height: 0; to height: auto; using CSS?
    How can I transition height: 0; to height: auto; using CSS? I am trying to make a  <ul>  slide down using CSS transitions. The ...
  • How to efficiently iterate over each Entry in a Map?
    How to efficiently iterate over each Entry in a Map? If I have an object implementing the  Map  interface in Java and I wish to iterate...
  • Event binding on dynamically created elements?
    Event binding on dynamically created elements? I have a bit of code where I am looping through all the select boxes on a page and bindi...
  • mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given
    mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given I am tr...
  • In Java, difference between default, public, protected, and private
    In Java, difference between default, public, protected, and private In Java , are there clear rules on when to use each of access modifi...
  • How to remove a property from a JavaScript object?
    How to remove a property from a JavaScript object? Say I create an object as follows: var myObject = { "ircEvent" : ...

statcounter



statcounter



Template Created By ThemeXpose & Blogger Templates