Date类型
ECMAScript 中的 Date 类型使用 UTC 国际协调时间,用从 1970年 1月 1日零时开始经过的毫秒数来保存日期,可以精确到 1970/1/1 之前或之后的 1亿年。
2.3.1 创建一个日期对象
var now = new Date();
console.log(now); //Fri Jul 21 2017 19:40:57 GMT+0800 (中国标准时间)
var now = Date.now(); //也是一样的
PS: 不同的浏览器或者不同的时区返回的结果会不同。
2.3.2 日期格式化方法
有以下几种方法,将 Date 类型格式化为字符串。
var now = new Date();
alert(now.toDateString()); //Fri Jul 21 2017
alert(now.toTimeString()); //19:50:52 GMT+0800 (中国标准时间)
alert(now.toLocaleDateString()); //2017/7/21
alert(now.toLocaleTimeString()); //下午7:52:34
alert(now.toUTCString()); //Fri, 21 Jul 2017 11:53:12 GMT
2.3.3 组件方法
var now = new Date();
var year = now.getFullYear(); //2017
var month = now.getMonth(); //6
var date = now.getDate(); //21
var day = now.getDay(); //5
var hour = now.getHours(); //20
var minute = now.getMinutes(); //12
var second = now.getSeconds(); //3