murat
murat

Reputation: 31

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

Upvotes: 1

Views: 167

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196236

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.

Upvotes: 3

Flipster
Flipster

Reputation: 4401

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);    

Upvotes: 0

Related Questions