jquery实现显示当前系统最近六个月的时间
时间:2021-07-01 10:21:17
帮助过:108人阅读
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<title>时间</title>
<script type="text/javascript">
$(function(){
initDate();
});
//格式化日期函数
function formateDate(date){
if(date instanceof Date){
var time=date.getMonth()+1;
var day=date.getDate();
if(day==31){
var myDate = new Date(date.getFullYear(),date.getMonth(), 0);
date.setDate(myDate.getDate());
time=time-1;
if(time==0){
time=time+1;
}
}
if(time<10){
time="0"+time;
}
return date.getFullYear()+""+time;
} else {
return "Error Date";
}
}
//页面加载完成后,执行初始化操作
function initDate(){
//当前日期
var date = new Date();
//获取select元素
$("#dateSelect").empty().append("<option value=''></option>");
for (var i = 0; i < 6; i++) {
$("#dateSelect").append(
"<option value='" + formateDate(date) + "'>"
+ formateDate(date) + "</option>");
//日期减一个月
date.setMonth(date.getMonth() - 1);
}
};
</script>
</head>
<body>
<div style="width:600px;height: 600px">
<select name="accountDate" id="dateSelect" class="form-control select2" style="width: 100%;">
</select>
</div>
</body>
</html>