最近,总是遇到Date的应用,这里小结一下:
Date 是 javascript中的对象
1 复制Date对象:
var birthday = new Date(1994, 12, 10);
var copy = new Date();
copy.setTime(birthday.getTime());
//这里需要复习的有,Date的初始化方法,
getTime, setTime 操作的都是毫秒数.
2 测量代码的执行时间
var end, start, i;
start = new Date();
for (i = 0; i < 1000; i++) {
Math.sqrt(i);
}
end = new Date();
console.log("Operation took " + (end.getTime() - start.getTime()) + " msec");
3 程序暂停特定时间
/* Wait for 5 seconds. */
var start = (new Date()).getTime();
while ((new Date()).getTime() - start < 5000);