欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Js时间差计算 博客分类: Js javascript 

程序员文章站 2024-02-18 20:05:40
...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js时间计算</title>
<script language="javascript" type="text/javascript">
<!--
function startToEndDate(){
var end_date = new Date();
var start_date = new Date();
start_date.setTime(end_date.getTime()-24*60*60*1000);

var start_year = start_date.getFullYear();
//getMonth中,以0为基数计算
var start_month = addZero(start_date.getMonth()+1);
var start_days = addZero(start_date.getDate());
var end_years = end_date.getFullYear();
var end_month = addZero(end_date.getMonth()+1);
var end_days = addZero(end_date.getDate());
var hours = addZero(end_date.getHours());
var minutes = addZero(end_date.getMinutes());
var seconds=addZero(end_date.getSeconds());

startDate = start_year+'-'+start_month+'-'+start_days+' '+hours+':'+minutes+':'+seconds;
endDate = end_years+'-'+end_month+'-'+end_days+' '+hours+':'+minutes+':'+seconds;
document.write("<h2>"+"startDate:"+startDate+",endDate:"+endDate+"</h2>");
}

function addZero(temp){
if(temp<10) return "0"+temp;
else return temp;
}
//-->
</script>
</head>

<body>
<script language="javascript" type="text/javascript">
startToEndDate();
</script>
</body>
</html>
相关标签: javascript