語法
var Time = new Date()日期/時間的組件方法:
Date.setTime() // 設置毫秒時間
Date.setFullYear() // 設置日期的年份,必須是4位數(shù)
Date.setMonth() // 設置日期的月份, 傳入的月份值必須大于0,超過11則增加年份 。
Date.setDate() // 設置日期月份中的天數(shù),如果傳入的值超過了該月中的天數(shù),則增加月份 。
Date.setDay() // 沒有這個方法
Date.setHours() // 設置日期中的小時數(shù),傳入的數(shù)值超過23則增加月份中的天數(shù) 。
Date.setMinutes() // 設置日期中的分鐘數(shù),傳入的值超過59則增加小時數(shù)
Date.setSeconds() // 設置日期中的秒數(shù),傳入的值超過59則增加分鐘數(shù)
Date.setMilliseconds() // 返回日期中的毫秒數(shù)獲取時間
var date = new Date();
console.log(date);
// 返回毫秒時間
console.log(date.getTime());
// 返回四位數(shù)的年份
console.log(date.getFullYear());
// 返回日期的月份 0 表示 一月 ;
console.log(date.getMonth());
// 返回日期月份的天數(shù) (1-31)
console.log(date.getDate());
// 返回日期的周幾 0 表示 星期日 ;
console.log(date.getDay());
// 返回日期中的小時數(shù) 0-23
console.log(date.getHours());
// 返回日期中的分鐘數(shù) 0-59
console.log(date.getMinutes());
// 返回日期中的秒數(shù) 0-59
console.log(date.getSeconds());
二、 定時器
設置定時器
var timer = setInterVal();//會不停調(diào)用
var timer = setTimeout(函數(shù)表達式,毫秒數(shù)) // 只執(zhí)行一次
清除定時器
clearInterval(timer);
clearTimeout(timer)
timer = null;
例如:
var i = 0;
var timer = setInterVal(function () {
// 每秒執(zhí)行一次
i++;
console.log(i);
if (i === 5) {
clearInterval(timer);
timer = null;
}
}, 1000);
var timer2 = setTimeout(function () {
console.log('tianchu');
clearTimeout(timer2);
timer2 = null;
}, 2000);