计算两个时间日期相差的天数、分钟数或者秒数

快乐打工仔 分类:实例代码

分享一段代码实例,它实现了计算两个时间日期相差的天数等时间差。

代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>犀牛前端部落</title>
<style type="text/css">
.st-div {
margin: 10px;
}
input {
width: 40px;
}
h2 {
margin-left: 200px;
}
.result {
margin: 10px;
}
</style>
</head>
<body>
<div class="st-div">
<label for="">从:</label><input id="sy" type="text" value="2012" /><label for="sy">年</label>
<input id="sm" type="text" value="03" /><label for="sm">月</label>
<input id="sd" type="text" value="01" /><label for="sd">日</label>
<span>--</span>
<input id="ey" type="text" value="2016" /><label for="ey">年</label>
<input id="em" type="text" value="03" /><label for="em">月</label>
<input id="ed" type="text" value="23" /><label for="ed">日</label>
<button>计算</button>
</div>
<div class="result">
<label for="">结果:<span id="answer"></span></label>
</div>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$('button').on('click', function() {
var st = $('#sy').val() + $('#sm').val() + $('#sd').val();
var et = $('#ey').val() + $('#em').val() + $('#ed').val();
var obj = {};
obj = new countDay(st, et);
var html = obj.totalDays + '天 ' + obj.totalMins + '分 ' + obj.totalSeconds + '秒';
$('#answer').html(html);
});
$('input').on('change', function() {
var va = $(this).val()
var len = va.length;
if (len > 0 && len < 4) {
if (va > 0 && va < 10) {
$(this).val('0' + va)
}
}
});
/***================================***/
var countDay = function(s, e) {
//验证输入的日期正确性
this.startDay = parseInt(s);
this.endDay = parseInt(e);
this.totalDays = 0;
this.startYear = parseInt(s.substring(0, 4));
this.endYear = parseInt(e.substring(0, 4));
this.totalDays = (function() {
var total = 0;
var sy = parseInt(s.substring(0, 4));
var ey = parseInt(e.substring(0, 4));
var sm = parseInt(s.substring(4, 6));
var em = parseInt(e.substring(4, 6));
var sd = parseInt(s.substring(6));
var ed = parseInt(e.substring(6));
var i = sy;
for (; i <= ey; i++) {
if (i === sy && i !== ey) {
total += countDayByYear(i, 1, sm, sd);
} else if (i === ey) {
total += countDayByYear(i, 2, em, ed);
} else {
total += countDayByYear(i, 0);
}
}
return total;
})();
this.totalMins = this.totalDays * 60;
this.totalSeconds = this.totalMins * 60;
return this;
};
function countDayByYear(y, flag) {
var ydm, s, e, tem = {};
if (!flag) {
ydm = 0;
s = 1;
e = 12;
} else if (flag === 1) {
var m = arguments[2];
var d = arguments[3];
tem = countDayByMonth(y, m);
ydm = tem.days - d + 1;
s = m + 1;
e = 12;
} else if (flag === 2) {
var m = arguments[2];
var d = arguments[3];
tem = countDayByMonth(y, m);
ydm = d;
s = 1;
e = m - 1;
}
for (; s <= e; s++) {
tem = countDayByMonth(y, s);
if (tem.days !== 0) {
ydm += tem.days;
}
}
return ydm;
};
function countDayByMonth(m) {
var obj = {},
d;
if (typeof m !== 'number' || m < 0 || m > 12) {
d = 0;
obj.errorMesaage = 'wrong type of parameter';
}
if (m === 1 || m === 3 || m === 5 || m === 7 || m === 8 || m === 10 || m === 12) {
d = 31;
} else if (m === 2) {
if (y % 4 === 0 && y % 100 !== 0) {
d = 29;
} else {
d = 28;
}
} else {
d = 30;
}
obj.days = d;
return obj;
}
</script>
</body>
</html>

计算两个时间日期相差的天数、分钟数或者秒数,这样的场景在实际项目中还是用的比较多的,关于计算两个时间日期相差的天数、分钟数或者秒数就介绍到这了。

计算两个时间日期相差的天数、分钟数或者秒数属于前端实例代码,有关更多实例代码大家可以查看

回复

我来回复
  • 暂无回复内容