javaScript自定义格式返回系统时间与倒计时

 function gettime(){
     var date=new Date();
     var year=date.getFullYear();
     var month=date.getMonth();
     var day=date.getDate();

     var hour=date.getHours();
     var minute=date.getMinutes();
     var second=date.getSeconds();
     function addZero(time){
       return time>10?time:'0'+time;
     }

     return year+'-'+addZero(month)+'-'+addZero(day)+' '+addZero(hour)+':'+addZero(minute)+':'+addZero(second);
   }
   console.log(gettime());
 //1,拿到未来时间获取时间戳
   var end=+new Date('2019=10-15');//当前日期 2019-10-13
   //2,当前时间获取时间戳
   var now=+new Date();
   //3.时间差
   var time=parseInt((end-now)/1000);//秒
   //4,天
   var day=parseInt(time/60/60/24);
   console.log(day);
   //5,小时 不满24小时的
   var hour=parseInt(time/60/60)%24;
   console.log(hour);
   //6,分 拿到不足六十分的
   var minute=parseInt(time/60)%60;
   console.log(minute);
   //7,秒 拿到不足六十秒的
   var second=time%60;
   console.log(second);

   var str='距离抽奖日期还有:'+day+'天'+hour+'小时'+minute+'分'+second+'秒';
   console.log(str);

猜你喜欢

转载自www.cnblogs.com/f2ehe/p/11666290.html