Get timestamp

Asked

Viewed 1,220 times

8

I have two variables, one with the date and the other with the time. How can I get your timestamp of the two variables. Example:

var data = 02/01/2015;
var hora = 10:00:00;
var d = new Date(data + hora);
d.getTime();

3 answers

5


It’s just a matter of formatting the content. First you can’t have the date and time so loose, probably you have as string. And then you need to separate the two contents with a blank space.

var data = "02/01/2015";
var hora = "10:00:00";
var d = new Date(data + " " + hora);
console.log(d.getTime());

Behold working in the Jsfiddle. Also put on the Github for future reference.

4

1

1st form

function TimeStamp(){
   return new Date().getTime();
} alert(TimeStamp());

Second form

function TimeStamp() {
    var d = new Date();
    var timestamp = d.getTime();
    return timestamp;
} alert(TimeStamp());

3rd form

function TimeStamp() {
    return event.timeStamp;
} alert(TimeStamp());

Important! If you use getTime() to get the real timestamp you should do the conversion below.

Math.round((new Date()).getTime() / 1000);

The getTime() returns milliseconds since the time UNIX, then divide it by 1000 to get the seconds representation. It is rounded using Math.round() to make it an integer number. The result now has the timestamp UNIX the current date and relegating time to the user browser.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.