1 日期 Date
◆ 日期默认格式
★ Date对象允许您使用日期(年,月,日,小时,分,秒和毫秒)
(years, months, days, hours, minutes, seconds, and milliseconds)
★ 默认日期格式
Fri Oct 13 2017 10:34:56 GMT+0800 (中国标准时间)
星期五10月13日2017 10:34:56 GMT + 0800(中国标准时间)
1507862096454
以数字编写的日期,指定自1970年1月1日00:00:00起的毫秒数。
◆ 创建日期对象(4种方式)
★ new Date() // 创建一个包含当前日期的新日期对象
var d = new Date();
document.write(d) // Fri Oct 13 2017 11:10:58 GMT+0800 (中国标准时间)
★ new Date(milliseconds) // 毫秒
var d = new Date(86400000);
document.write(d) // Fri Jan 02 1970 08:00:00 GMT+0800 (中国标准时间)
// 周五1月02日1970 08:00:00 GMT + 0800(中国标准时间)
日期以1970年01月01日00:00:00世界时(UTC)计算。一天包含86,400,000毫秒。
★ new Date(dateString) // 日期字符串,从指定的日期和时间创建一个新的日期对象
var d = new Date("10-13, 2014 11:13:00");
document.write(d) // Mon Oct 13 2014 11:13:00 GMT+0800 (中国标准时间)
★ new Date(year, month, day, hours, minutes, seconds, milliseconds)
// 7个数字按照该顺序指定 年,月,日,小时,分,秒和毫秒
var d = new Date(2017, 4, 24, 6, 33, 30, 0); // 可省略后五位数 例如 (2017,4) 其他位数默认值
document.write(d);
// Wed May 24 2017 06:33:30 GMT+0800 (中国标准时间)
// 星期三 5月24日2017 06:33:30 GMT + 0800(中国标准时间)
//JavaScript计数从0到11的月份。1月是0月12日是11。实际月份比填入月份大1
◆ 显示日期
★ toString() // 默认方法将其自动转换为字符串
★ toUTCString() // 方法将日期转换为一个字符串UTC(日期显示标准)
var d = new Date(2017, 4, 24, 6, 33, 30, 0);
document.write(d.toUTCString()); // Tue, 23 May 2017 22:33:30 GMT //24日会变23日
★ toDateString() // 一致的方法将日期转换为一个更可读的形式
var d = new Date(2017, 4, 24, 6, 33, 30, 0);
document.write(d.toDateString()); // Wed May 24 2017
◆ 时区
设置日期时,不指定时区,JavaScript将使用浏览器的时区。
获取日期时,不指定时区,结果将转换为浏览器的时区。
换句话说:如果GMT(格林尼治标准时间)创建日期/时间,则如果用户从美国中部浏览,日期/时间将被转换为CDT(美国中部时间)。