简体   繁体   中英

convert milisecons to normal date

function openAPage() {
var startTime = new Date().getTime();
var myWin = window.open("http://www.sabah.com.tr","_blank")
var endTime = new Date().getTime();
var timeTaken = endTime-startTime;
myWin.close()

document.write(startTime);
document.write(endTime);
document.write(timeTaken);    
}

hi i want to see the date here "document.write(startTime);".. how can i convert

document.write(  new Date(startTime) );

If you check the documentation for the Date object one of the constructors is

new Date(milliseconds)

This way you recreate a Date from the milliseconds passed as argument.
It counts milliseconds since 1 January 1970 00:00:00 UTC .

But keep in mind that the window.open will not wait until the window has loaded before continuing execution of the code. So your startTime and endTime variables will be always pretty close.

Create your startTime with both a time and date and all will be well.

Like this:

var startTime = new Date().getDate();
var myWin = window.open("http://www.sabah.com.tr","_blank")
var endTime = new Date().getDate();
var timeTaken = endTime-startTime;

You can still determine the time difference (elapsed time) between them, but will also have the date available to the end of your routine.

document.write(startTime);
document.write(endTime);
document.write(timeTaken);    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM