由于需求需要做中秋节抽奖 所以有一个倒计时,顺手记录下来了
可以把时间date对象转化成时间戳 根据时间戳来得出时间差
function countdown() {
let nowDate = new Date();
let nowDateChuo = Date.parse(nowDate); //转化成时间戳
let midAutumn = new Date('2019-9-13 12:00:00');
let midAutumneChuo = Date.parse(midAutumn);
let date3 = midAutumneChuo - nowDateChuo;
var days = Math.floor(date3 / (24 * 3600 * 1000));
var leave1 = date3 % (24 * 3600 * 1000);
var hours = Math.floor(leave1 / (3600 * 1000));
var leave2 = leave1 % (3600 * 1000);
var minutes = Math.floor(leave2 / (60 * 1000));
var leave3 = leave2 % (60 * 1000);
var seconds = Math.round(leave3 / 1000);
if (hours < 10) hours = '0' + hours;
if (minutes < 10) minutes = '0' + minutes;
if (seconds < 10) seconds = '0' + seconds;
$('.day').text(days);
$('.hour').text(hours);
$('.minutes').text(minutes);
$('.second').text(seconds);
}
setInterval(countdown, 1000);
还有一种是根据年月日来计算 我这里没写
顺带记一下判断某个日期星期几 date提供了getDay()
var date = "08/28/2019"; //此处也可以写成 17/07/2014 一样识别 也可以写成 07-17-2014 但需要正则转换
var day = new Date(Date.parse(date)); //需要正则转换的则 此处为 : var day = new Date(Date.parse(date.replace(/-/g, '/')));
var today = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');//新建一个对应的中文星期数组
var week = today[day.getDay()];
//最终结果为:
console.log(week);
网友评论