Date( )日期对象是一个构造函数,必须使用new来调用创建日期对象
使用new来创建日期对象
1.如果没有参数,返回系统的当前时间
var date = new Date();
console.log(date);
2.参数常用的写法
1)数字型 2019,08,01
var date1 = new Date(2019,08,01);
console.log(date1);//返回的是9月,不是8月Sun Sep 01 2019 00:00:00 GMT+0800 (中国标准时间)
使用数字型写法,返回时月份会加1
2)字符串型 ‘2019-08-01 15:34:29 ’
var date2 = new Date('2019-08-01 15:34:29');
console.log(date2)//Thu Aug 01 2019 15:34:29 GMT+0800 (中国标准时间)
日期格式化
方法名 | 说明 |
---|---|
getFullYear( ) | 获取当年 |
getMonth( ) | 获取当月(0-11) |
getDate( ) | 获取当天日期 |
getDay( ) | 获取星期几(周日0到周六6) |
getHours( ) | 获取当前小时 |
getMinutes( ) | 获取当前分钟 |
getSeconds( ) | 获取当前秒中 |
示例:
var date = new Date();
console.log(date.getFullYear());//返回当前年份2019
console.log(date.getMonth() + 1);//8返回值比当前月份小1,所以获得当前月份需加1
console.log(date.getDate());//1返回几号
console.log(date.getDay());//4返回当前星期几 注意:周日返回0
console.log(date.getHours());//15返回当前小时
console.log(date.getMinutes());//51返回当前分钟
console.log(date.getSeconds());//26返回当前秒
日期格式化为:今天是:2019年8月1日星期四
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;//记住月份要+1
var dates = date.getDate();
var day = date.getDay();
var hour = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
onsole.log('今天是:' + year + '年' + month + '月' + dates + '日' + '星期' + day+ hour + ':' + minutes + ':' + seconds);
//今天是:2019年8月1日星期416:12:18
上面案例有两个问题,显然,星期4的写法不如星期四,且万一是周日,则会显示星期0。
为解决这个问题,可以使用数组同时解决这两个问题。
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var dates = date.getDate();
var hour = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var arr = ['星期日' ,'星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
var day = date.getDay();
console.log('今天是:' + year + '年' + month + '月' + dates + '日' + arr[day] + hour + ':' + minutes + ':' + seconds);
// 今天是:2019年8月1日星期四16:18:3
看起来已经很完美了,但还有一个小问题,我们希望当月份,日期以及时分秒在不满10的时候都在前面能加上一个0,16:18:03看上去会比16:18:3好很多。
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = month < 10 ? '0' + month : month;
var dates = date.getDate();
dates = dates < 10 ? '0' + dates : dates;
var hour = date.getHours();
hour = hour < 10 ? '0' + hour : hour;
var minutes = date.getMinutes();
minutes = minutes < 10 ? '0' + minutes : minutes;
var seconds = date.getSeconds();
seconds = seconds < 10 ? '0' + seconds : seconds;
var arr = ['星期日' ,'星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
var day = date.getDay();
console.log('今天是:' + year + '年' + month + '月' + dates + '日' + arr[day] + hour + ':' + minutes + ':' + seconds);
//今天是:2019年08月01日星期四16:22:03
分别给月日时分秒都加上一个三目运算符,要是小于10,则在前面添0。
获取日期的总的毫秒形式(时间戳)
Date对象是基于1970年1月1日起的毫秒数,用毫秒数计算时间更加精准。
valueOf( )和getTime( )
var date = new Date();
console.log(date.valueOf());//1564649084891
console.log(date.getTime());//1564649084891
简单方法(常用)
var date = +new Date();
console.log(date);//1564649359290
H5新增方法
console.log(Date.now());
***案例:倒计时效果
分析
核心算法:
1.输入的时间减去现在的时间就是剩余时间,即倒计时,但是不能用时分秒想减,05分减去25分则会产生负数。
2.可用时间戳进行计算。
3.把时间戳转换为天,时,分,秒。
function countDown(time){
var nowTime = +new Date();//现在的毫秒数
var inputTime = +new Date(time);//用户输入的毫秒数
var times = (inputTime - nowTime) / 1000;//相减除以1000得到秒数
var d = parseInt(times / 60 / 60 / 24);//转换为天
var h = parseInt(times / 60 / 60 % 24);//转换为时
var m = parseInt(times / 60 % 60);//转换为分
var s = parseInt(times % 60);//转换为秒
d = d < 10 ? '0' + d : d;//小于10,前面添0
h = h < 10 ? '0' + h : h;
m = m < 10 ? '0' + m : m;
s = s < 10 ? '0' + s : s;
return d + '天' + h + '时' + m + '分' + s + '秒'
}
console.log('剩余时间:' + countDown('2019-8-3 20:0:0'));
//剩余时间:02天02时37分26秒