倒计时:
    倒计时

    1. function cutTime(time,id){
    2. var time = time.replace(/-/g,"/");
    3. var EndTime= new Date(time);
    4. var NowTime = new Date();
    5. var t =EndTime.getTime() - NowTime.getTime();
    6. var d=Math.floor(t/1000/60/60/24);
    7. var h=Math.floor(t/1000/60/60%24);
    8. var m=Math.floor(t/1000/60%60);
    9. var s=Math.floor(t/1000%60);
    10. h = checkTime(h);
    11. m = checkTime(m);
    12. s = checkTime(s);
    13. $("#th"+id).html(h);
    14. $("#tm"+id).html(m);
    15. $("#ts"+id).html(s);
    16. }
    17. setInterval("cutTime('2017-12-12 10:00:00',1)",1000);

    日期格式转换:

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