js获取当前时间、计算时间差、倒计时

  • 获取当前时间
getDate(){
	let myDate = new Date();
    let year = myDate.getFullYear();
    let month = myDate.getMonth() + 1;
    month = month >= 10? month : '0'+month;
    let date = myDate.getDate();
    date = date >= 10? date : '0'+date;
    let h = myDate.getHours();
    h = h >= 10? h : '0'+h;
    let m = myDate.getMinutes();
    m = m >= 10 ? m : '0'+m;
    let now = year + '/' + month + "/" + date + " " + h + ':' + m;
    return now;
},
 var dateFormat = function (timestamp, formats) {
        // formats格式包括
        // 1. Y-m-d
        // 2. Y-m-d H:i:s
        // 3. Y年m月d日
        // 4. Y年m月d日 H时i分
        formats = formats || 'Y-m-d';

        var zero = function (value) {
            if (value < 10) {
                return '0' + value;
            }
            return value;
        };

        var myDate = timestamp? new Date(timestamp): new Date();

        var year = myDate.getFullYear();
        var month = zero(myDate.getMonth() + 1);
        var day = zero(myDate.getDate());

        var hour = zero(myDate.getHours());
        var minite = zero(myDate.getMinutes());
        var second = zero(myDate.getSeconds());

        return formats.replace(/Y|m|d|H|i|s/ig, function (matches) {
            return ({
                Y: year,
                m: month,
                d: day,
                H: hour,
                i: minite,
                s: second
            })[matches];
        });
    };
    setInterval(()=>{
        console.log(dateFormat(new Date,'Y年m月d日 H时i分'))
    },1000)
  • 计算时间差
//faultDate=>当前时间、completeTime=>目标时间.
DateDifference(faultDate,completeTime){
	   var stime =new Date(faultDate).getTime();
	   var etime = new Date(completeTime).getTime();
	   var usedTime = etime - stime;  //两个时间戳相差的毫秒数
	   var days=Math.floor(usedTime/(24*3600*1000));
	   //计算出小时数
	   var leave1=usedTime%(24*3600*1000);    //计算天数后剩余的毫秒数
	   var hours=Math.floor(leave1/(3600*1000));
	   //计算相差分钟数
	   var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
	   var minutes=Math.floor(leave2/(60*1000));
	   var time = hours;
	   return time;
},
  • 倒计时
  var showtime = function () {
        var nowtime = new Date(); //获取当前时间
        var endtime = new Date("2020/5/13");  //定义结束时间
        var lefttime = endtime.getTime() - nowtime.getTime(),  //距离结束时间的毫秒数
        leftd = Math.floor(lefttime/(1000*60*60*24)),  //计算天数
        lefth = Math.floor(lefttime/(1000*60*60)%24),  //计算小时数
        leftm = Math.floor(lefttime/(1000*60)%60),  //计算分钟数
        lefts = Math.floor(lefttime/1000%60);  //计算秒数
        return leftd + ":" + lefth + ":" + leftm + ":" + lefts;  //返回倒计时的字符串
  };
  setInterval(()=>{
  	console.log(showtime());
  })
  /*一下代码属于自娱自乐,可以忽略*/
    function dtime(fun){
        let time = setInterval(()=>{
           let a = showtime();
           let b,c,d;
           b = a.replace(/:/g,'');
           c = b.split(',');
           d = eval(c.join('+'));
           fun.call(this,a);
           if(d == 0){
                clearInterval(time);
           }
        },1000)
    };
    dtime((a)=>{
        console.log(a)
    })
  • 开源的项目 http://www.lib4dev.in/topics/javascript

你可能感兴趣的:(js获取当前时间、计算时间差、倒计时)