-
- var time = new Date();
- console.log(time.getFullYear());//年
- console.log(time.getMonth()+1);//月,这个地方要加1才是现在的月份
- console.log(time.getDate());//日期
- console.log(time.getHours());//小时
- console.log(time.getMinutes());//分钟
- console.log(time.getSeconds());//秒
-
-
-
-
也可以直接赋值:
var time = new Date(2019,1,2);
或者:
var time = new Date([2019,1,2]);
最标准:
var time = new Date('2019-1-2 12:00:00');
注意 :第二种写法,月份会自动减1
四种方法
-
- var time = new Date();
-
- console.log(time.valueOf());
-
- console.log(time.getTime());
-
- var data = +new Date();
- console.log(data);
-
- console.log(Date.now());
-
-
- var time = new Date('2019-1-2 12:00:00');
- function count(time){//输入数组 例如一天两个小时10分钟 [1,2,10]
- var now = +new Date();
- var set = +new Date(time);
- var add = (set - now)/1000; //相差的秒数
- var day = parseInt(add/24/60/60);
- var hour = parseInt(add/60/60&24);
- var min = parseInt(add/60%60);
- var sec = parseInt(add%60);
- return day+'天'+hour+'时'+min+'分'+sec+'秒';
- }
- console.log(count('2022-10-2 12:00:00'));
-
-