倒计时:
倒计时
function cutTime(time,id){
var time = time.replace(/-/g,"/");
var EndTime= new Date(time);
var NowTime = new Date();
var t =EndTime.getTime() - NowTime.getTime();
var d=Math.floor(t/1000/60/60/24);
var h=Math.floor(t/1000/60/60%24);
var m=Math.floor(t/1000/60%60);
var s=Math.floor(t/1000%60);
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
$("#th"+id).html(h);
$("#tm"+id).html(m);
$("#ts"+id).html(s);
}
setInterval("cutTime('2017-12-12 10:00:00',1)",1000);
日期格式转换:
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
function getBeginTime(time){
var time = time.replace(/-/g,"/");
var beginTime= new Date(time);
var begin = beginTime.Format("MM年dd日 hh:mm");
return begin;
}
//方法中,若传入参数time的格式为:2017-01-01 00:00:00,则需要time.replace(/-/g,”/”);将日期格式化为毫秒模式,若time格式本身为毫秒,则无需此方法转换。
// beginTime.Format(“MM年dd日 hh:mm”)即为调用之前的基础方法,实现日期转换,format里面的字符串可以一般Java方法中参数的来,例如“yyyy年MM月dd日 hh:mm:ss” 。
// moment.js
setInterval(() => {
let start = moment(new Date());//获取开始时间
let end = moment(new Date("2020/12/20"));//结束时间
let diff = end.diff(start);//时间差
let time = `${moment.duration(diff).hours()} - ${moment.duration(diff).minutes()} - ${moment.duration(diff).seconds()}`//格式化为需要的格式 这里是时分秒
console.log(time)
}, 1000)