记得引入 jquery.js
<!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>
</head>
<body>
计算从今天开始算 7天前的年月日<br/>
<input type="text" id="time" value="7" />
<br/>
<label id="showTime"></label>
<br/>
<input type="button" id="btn" value="计算"/>
<script type="text/javascript" src="../public/js/jquery-2.0.3.min.js"></script>
<script>
$(function(){
$("#btn").click(function(){
var timeLength=parseInt($("#time").val())
$("#showTime").html(time.getPreDate(timeLength))
})
var time={
getPreDate:function(pre){
var self=this;
var c = new Date();
c.setDate(c.getDate() - pre);
return self.formatDate(c);
},
formatDate:function(d){
var self=this;
return d.getFullYear() + "-" + self.getMonth1(d.getMonth()) + "-" + self.getMonth1(d.getDate(),"getDate")
},
getMonth1:function(m,type){
var self=this;
if(type!="getDate"){
m++;
}
if(m<10)
return "0" + m.toString();
return m.toString();
}
}
})
</script>
</body>
</html>
前端交流群 欢迎加入