java中java.util.Date类基础上构建的。
Date类型使用自UTC(Coordinated Universal Time,国际协调时间)1970年1月1日午夜(零时)开始经过的毫秒数来保存日期.
Date.UTC
var minutes = 1000 * 60;
var hours = minutes * 60;
var days = hours * 24;
var years = days * 365;
var t = Date.UTC(2005,7,8);
var y = t/years;
document.write("It's been: " + y + " years from 1970/01/01");
document.write(" to 2005/07/08!");
Date
语法: new Date(dateStr)
dateStr格式
// "2016/7/8 上午12:00:00"
new Date('7/8/2016 ').toLocaleString();
// "2016/7/8 上午12:00:00"
new Date('July 8,2016').toLocaleString();
// "2016/7/8 上午11:00:00"
new Date('Friday July 8 2016 11:00:00').toLocaleString();
// "2016/7/8 上午12:00:00"
new Date('Friday July 8 2016').toLocaleString();
// "2016/7/8 上午11:00:00"
new Date('Friday July 8 2016 11:00').toLocaleString();
// "2001/7/8 上午12:00:00"
new Date('Friday July 8').toLocaleString();
// "2016/7/8 上午11:00:00"
new Date('July 8 2016 11:00').toLocaleString();
备注
Date.now
备注
if (!Date.now) {
Date.now = function now() {
return new Date().getTime();
};
}
var d = new Date();
d.toISOString();
将日期格式化为字符串的方法:
toDateString
var d = new Date()
document.write(d.toDateString());
toTimeString
var d = new Date()
document.write(d.toTimeString());
toLocaleDateString
var d = new Date()
document.write(d.toLocaleDateString());
toLocaleTimeString
var d = new Date()
document.write(d.toLocaleTimeString());
toUTCString
var d = new Date()
document.write(d.toUTCString());
toLocaleString和toString方法一样 以上这些字符串格式方法的输出也是因浏览器而异的
getTime
var minutes = 1000*60;
var hours = minutes*60;
var days = hours*24;
var years = days*365;
var d = new Date();
var t = d.getTime();
var y = t/years;
document.write("It's been: " + y + " years since 1970/01/01!");
setTime
var date = new Date('7/6/2016');
date.setTime('1467948764748');
// "2016/7/8"
date.toLocaleDateString();
getFullYear
var d = new Date();
document.write(d.getFullYear());
getUTCFullYear
var d = new Date();
document.write(d.getUTCFullYear());
setFullYear
var date = new Date('7/8/2016');
date.setFullYear('2017');
// "2017/7/8"
date.toLocaleDateString();
date.setFullYear(2018);
// "2018/7/8"
date.toLocaleDateString();
var d = new Date();
d.setFullYear(1992,10,3);
document.write(d);
setUTCFullYear
var date = new Date('7/6/2016');
date.setUTCFullYear('2017');
// "2017/7/6"
date.toLocaleDateString();
date.setUTCFullYear(2018);
// "2018/7/6"
date.toLocaleDateString();
var d = new Date();
d.setUTCFullYear(1992,10,3);
document.write(d);
getMonth
var d=new Date();
var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
document.write("The month is " + month[d.getMonth()]);
getUTCMonth
var d=new Date();
var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
document.write("The month is " + month[d.getUTCMonth()]);
setMonth
var d=new Date();
d.setMonth(0);
document.write(d);
setUTCMonth
var d=new Date();
d.setUTCMonth(0);
document.write(d);
getDate
var d = new Date();
document.write(d.getDate());
getUTCDate
var d = new Date();
document.write(d.getUTCDate());
setDate
var d = new Date();
d.setDate(15);
document.write(d);
setUTCDate
var d = new Date();
d.setDate(15);
document.write(d);
getDay
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
document.write("Today it is " + weekday[d.getDay()]);
getUTCDay
var d=new Date();
document.write(d.getUTCDay());
getHours
var d = new Date();
document.write(d.getHours());
getUTCHours
var d = new Date();
document.write(d.getUTCHours());
setHours
var d = new Date();
d.setHours(15,35,1);
document.write(d);
setUTCHours
var d = new Date();
d.setUTCHours(23,15,6);
document.write(d);
getMinutes
var d = new Date();
document.write(d.getMinutes());
getUTCMinutes
var born = new Date("July 21, 1983 01:15:00");
document.write(born.getUTCMinutes());
setMinutes
var d = new Date();
d.setMinutes(1);
document.write(d);
setUTCMinutes
var d = new Date();
d.setUTCMinutes(1);
document.write(d);
getSeconds
var d = new Date();
document.write(d.getSeconds());
getUTCSeconds
var Birthday = new Date("July 21, 1983 01:15:00");
document.write(Birthday.getUTCSeconds());
setSeconds
var d = new Date()
d.setSeconds(1)
document.write(d)
setUTCSeconds
var d = new Date()
d.setSeconds(1)
document.write(d)
getMilliseconds
var d = new Date();
document.write(d.getMilliseconds());
getUTCMilliseconds
var d = new Date()
document.write(d.getUTCMilliseconds())
setMilliseconds
var d = new Date()
d.setMilliseconds(1)
document.write(d)
setUTCMilliseconds
var d = new Date()
d.setUTCMilliseconds(1)
document.write(d)
getTimezoneOffset
var gmtHours = d.getTimezoneOffset()/60
document.write("The local time zone is: GMT " + gmtHours)
return new Date(NaN) + "" === "Invalid Date";