Translate

Friday, May 15, 2015

Convert a date into Timestamp in Javscript



To convert a date into timestamp milisecond  in JavaScript we are going to use the function new Date() that JavaScript provide  to deal with dates.

Basically, this is code:


//convert date into a timestamp milisecond
var oDate = new Date('2015','05','12','07','30','00');

//printing the timestamp   
console.log(oDate.getTime());


Explanation:

The function  Date() , receives the following parameters:
new Date(year, month, day, hours, minutes, seconds, milliseconds)

so,the Object oDate(Variable) has a method called getTime, that you can access to convert the date into a timeStamp.


Output:

1434115800000


NOTE: In javascript, the conversion to timestamp is in timestamp millisecond and not timestamp epoch. 


No comments:

Post a Comment