Js计算时间差

 

                            var runTime = (t1.getTime() - t2.getTime())/1000;
                            var year = Math.floor(runTime / 86400 / 365);
                            runTime = runTime % (86400 * 365);
                            var month = Math.floor(runTime / 86400 / 30);
                            runTime = runTime % (86400 * 30);
                            var day = Math.floor(runTime / 86400);
                            runTime = runTime % 86400;
                            var hour = Math.floor(runTime / 3600);
                            runTime = runTime % 3600;
                            var minute = Math.floor(runTime / 60);
                            runTime = runTime % 60;
                            var second = runTime;

                            var res = '';
                            if(month > 0){
                                res = month + '月'
                            }
                            if(day > 0){
                                res = day + '天'
                            }
                            if(hour > 0){
                                res = hour + '时'
                            }
                            if(minute > 0){
                                res = minute + '分'
                            }
                            if(second > 0){
                                res = second + '秒'
                            }

 

你可能感兴趣的:(Js计算时间差)