One version of the Date constructor takes the number of "milliseconds since Unix Epoch" as its first and only parameter.
Assuming your timestamp is in a variable called domTimeStamp , the following code converts that timestamp to local time (assuming that the user has the correct date and time zone on his / her machine) and print a readable version of date:
var d = new Date(domTimeStamp); document.write(d.toLocaleString());
Other built-in date formatting methods include:
Date.toDateString() Date.toLocaleDateString() Date.toLocaleTimeString() Date.toString() Date.toTimeString() Date.toUTCString()
Assuming your requirement is to print the exact "HH: MM: SS MM-DD-YY" template, you could do something like this:
var d = new Date(domTimeStamp); var hours = d.getHours(), minutes = d.getMinutes(), seconds = d.getSeconds(), month = d.getMonth() + 1, day = d.getDate(), year = d.getFullYear() % 100; function pad(d) { return (d < 10 ? "0" : "") + d; } var formattedDate = pad(hours) + ":" + pad(minutes) + ":" + pad(seconds) + " " + pad(month) + "-" + pad(day) + "-" + pad(year); document.write(formattedDate);
source share