var date = new Date();
//输出UTC时间
document.write(date + '</br>');
//输出本地日期、时间
document.write(date.toLocaleString() + '</br>');
//输出本地日期
document.write(date.toLocaleDateString() + '</br>');
//(1970年1.1凌晨0点)距离
document.write(date.getTime() + '</br>');
document.write(date.toLocaleString().replace('年', '-').replace('月', '-').replace('日', '') + '</br>');
var time = date.toLocaleDateString();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
document.write(time + '下午' +( (hour>12)?'下午'+hour:'上午'+hour) + '点' + minute + '分' + second + '秒');
//计算两时间差
var time1 = new Date(2012, 6, 3);
var time2 = new Date(2012, 3, 6);
//方法一
var time = Math.abs(time1 - time2) / 1000 / 3600;
document.write('相差' + time + '小时'+'</br>');
document.write('相差' + time / 24 + '天' + '</br>');
document.write('相差' + time / 24 / 30 + '个月' + '</br>');
document.write('-----------------</br>')
//方法二
time = (time1.getTime() - time2.getTime()) / 1000 / 3600;
document.write('相差' + time + '小时' + '</br>');
document.write('相差' + time / 24 + '天' + '</br>');
document.write('相差' + time / 24 / 30 + '个月' + '</br>');
var date = new Date();
date.setDate(date.getDate() - 2);
document.write('前天是:' + date.toLocaleString());