js时间之间的计算

自己写的时间
如:var startTime= ‘2019/12/06 19:00:00’;
只有这样 new Date(startTime).getTime()
才能把自己写的时间转化成毫秒数来进行加减乘除

获取当前时间

   SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String hh=simpleDateFormat.format(new Date());

毫秒数 利用Math.floor方法进行获取部分的时间

d=Math.floor(lefttime/1000/60/60/24); //天
h=Math.floor(lefttime/1000/60/60%24); //时
m=Math.floor(lefttime/1000/60%60); //分
s=Math.floor(lefttime/1000%60); //秒

新建的date可以直接用get获得部分的时间

var date=new Date();//获取当前时间(包括日期)
var year=date.getFullYear();
var month=date.getMonth()+1;
var day=date.getDate();
var hh=date.getHours();
var mm=date.getMinutes();
var ss=date.getSeconds();

一段时间运行一下这个方法
setTimeout(“disptime()”,1000)

<script type="text/javascript">
  
    window.onload=function(){
        a3();
    }

    //在number小于10的数组前补0
    function checkTime(i){
        if(i<10){
            i="0"+i;
        }
        return i;
    }

    //3
    function a3(){
        var date=new Date();//获取当前时间(包括日期)
        var startTime= '2019/12/06 19:00:00';  //开始时间
        var testTime="7500000";//两小时零五分 2*60*60+5*60
        var lefttime = testTime-(date-new Date(startTime).getTime());   //考试时间-(当前时间-开始时间)=倒计时

        var d,h,m,s;
        if(lefttime>0){
//            d=Math.floor(lefttime/1000/60/60/24);  //天
            h=checkTime(Math.floor(lefttime/1000/60/60%24));  //时
            m=checkTime(Math.floor(lefttime/1000/60%60));  //分
            s=checkTime(Math.floor(lefttime/1000%60));     //秒
//            lefttime=lefttime-1000; //每次进来将总时间减去1秒

            document.getElementById("sy").innerHTML=" "+h+":"+m+":"+s;

            var myTimer=setTimeout("a3()",1000);
        }else{
            document.getElementById("sy").innerHTML="跳转 ";
        }
    }
</script>
<body>
    <div style="margin-top:8px;">
                当前时间-开始时间: <font id="sy" color="red" ></font>
        </div>
</body>

在这里插入图片描述

参考于:https://www.cnblogs.com/zhaosq/p/10918757.html
https://blog.csdn.net/firstday2014/article/details/87876322

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值