本文目录一览:
JS计算时间差问题,求方法。。。
/**
* timeDiff(first,sencond,[pattern])
*/
var TimeDiff = (function (f) {
return function (a, b, p) {
return f(a, p) - f(b, p);
};
}
(function (t, p) {
return(function (h, m, s, ss, f) {
return f = parseInt, 1000 * (60 * (60 * f(h || 0) + f(m || 0)) + f(s || 0)) + f(ss || 0);
}).apply(0, (p || /^(\d\d):(\d\d):(\d\d)\.(\d\d\d)$/).exec(t).splice(1));
}));
TimeDiff("08:12:34.008", "08:12:34.007");//楼主格式
TimeDiff("9","8",/^()()(\d)()$/);//计算两个分钟时间的毫秒差
TimeDiff("12:34","13:34",/^()(\d+):(\d+)()$/);//计算分钟:秒格式的毫秒差
如果要格式化就很简单了吧
/**
* TimeFmt(毫秒,格式)
*/
var TimeFmt = function (t, f, d, i) {
for (i in d = {ss: 1000, s: 60, m: 60, h: 60}) {
f = f.replace(i, t % d[i]);
t = parseInt(t / d[i]);
}
return f;
};
TimeFmt(59999,"m.s");//这就是59.999
parseFloat(TimeFmt(59999,"m.s"));//转为float
TimeFmt(TimeDiff("08:12:34.008","08:12:32.252"),"s.ss");//
测试了一下,有个小bug,改正过了
用js怎么计算两个日期之间的时间差?
//+---------------------------------------------------
//| 求两个时间的天数差 日期格式为 YYYY-MM-dd
//+---------------------------------------------------
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
return Math.abs(cha);
}
js计算时间差代码
js求时间差
var date1=new Date(); //开始时间
alert("aa");
var date2=new Date(); //结束时间
var date3=date2.getTime()-date1.getTime() //时间差的毫秒数
//计算出相差天数
var days=Math.floor(date3/(24*3600*1000))
js怎么写一个时间相减得到时间差?
需要准备的材料分别有:电脑、html编辑器、浏览器。
1、首先,打开html编辑器,新建html文件,例如:index.html。
2、在index.html中的script标签,输入js代码:
var a = '2019-7-1 12:00:00', b = '2019-7-2 14:58:05';
var t1 = Date.parse(new Date(a.replace(/-/g, "/")));
var t2 = Date.parse(new Date(b.replace(/-/g, "/")));
document.body.innerText = b + ' 比 ' + a + ' 大 ' + (t2 - t1) + '秒';
3、浏览器运行index.html页面,此时会打印出2个时间相减得到的时间差。
js计算时间差,注意是时分
按照你的要求编写的计算时间差的Javascript程序如下(注意 date是随意写的正确的日期字符串,只是为了函数Date.parse()的需要)
script type=text/javascript
var date="2018/07/17";
var time1 = Date.parse(date+" "+"2:32:39");
var time2 = Date.parse(date+" "+"14:12:09");
var time3=time2-time1;
var hour=Math.floor(time3/1000/60/60);
var minute=Math.floor(time3/1000/60-hour*60);
var second=time3/1000-hour*60*60-minute*60;
alert("时间差为"+hour+"小时"+minute+"分"+second+"秒");
/script
运行结果
时间差为11小时39分30秒