简体   繁体   English

将milisecons转换为正常日期

[英]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(startTime);” ..我怎么转换

document.write(  new Date(startTime) );

If you check the documentation for the Date object one of the constructors is 如果检查Date对象文档 ,则构造函数之一是

new Date(milliseconds)

This way you recreate a Date from the milliseconds passed as argument. 这样,您可以从作为参数传递的毫秒数中重新创建一个Date。
It counts milliseconds since 1 January 1970 00:00:00 UTC . 自1970年1月1日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. 但是请记住,在继续执行代码之前, window.open不会等到窗口加载完毕。 So your startTime and endTime variables will be always pretty close. 因此,您的startTimeendTime变量将始终非常接近。

Create your startTime with both a time and date and all will be well. 创建一个带有时间和日期的startTime,一切都会很好。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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