js-时间运算与格式
Posted 儿一愿,娘一生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js-时间运算与格式相关的知识,希望对你有一定的参考价值。
注意事项:1.时间没有格式函数;
2.将时间都转换为毫秒来计算;
3.1秒=1000毫秒。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>时间运算-加减</title>
<script type="text/javascript">
window.onload = function () {
var dateStr = document.getElementById("myDate").value;
var MinuteStr = eval(document.getElementById("mySeconds")).value;
var resultStr = dateAddMinute(dateStr,MinuteStr);
//alert("结果:"+resultStr)
}
//日期加分钟
function dateAddMinute(pDate,pMinute) {
var date = new Date(pDate);
var resultDate = new Date();
var resultStr ; //返回值
date = date.getTime() + pMinute*60*1000;
resultDate.setTime(date);
var year = resultDate.getFullYear();
var month = resultDate.getDay() + 1;
var day = resultDate.getDay();
var hours = resultDate.getHours();
var minutes = resultDate.getMinutes();
var seconds = resultDate.getSeconds();
month = month<9?"0"+month:month;
day = day<9?"0"+day:day;
hours = hours<9?"0"+hours:hours;
minutes = minutes<9?"0"+minutes:minutes;
seconds = seconds<9?"0"+seconds:seconds;
resultStr = year + "-" + month + "-" + day +
" " + hours + ":" + minutes + ":" + seconds;
return resultStr;
}
function btnOnClick() {
var dateStr = document.getElementById("myDate").value;
var MinuteStr = eval(document.getElementById("mySeconds")).value;
var resultStr = dateAddMinute(dateStr,MinuteStr);
alert("结果:"+resultStr)
}
</script>
</head>
<body>
<h1>时间运算-加减</h1>
日期:<input id="myDate" type="text" name="myDate" value="2016-06-20 17:01:02">
分钟:<input id="mySeconds" type="text" name="mySeconds" value="2">
<button onclick="btnOnClick()">相加</button>
结果:<input id="resultStr" type="text" name="resultStr" >
</body>
</html>
以上是关于js-时间运算与格式的主要内容,如果未能解决你的问题,请参考以下文章