JavaScript实现的一个设置时间有效期的,自己用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>无标题文档</title>
<style type="text/css">
#lastdate{ color:#FF9900; font-style:normal;}
</style>
</head>
<body>
<div class="f-c">
<select id="date_sel" name="period" class="buyConQc select150">
<option value="0">一天内</option>
<option value="1">两天内</option>
<option value="2">三天内</option>
<option value="3">四天内</option>
</select>
<span class="postAction">在 <em id="lastdate">2012-9-28</em> 后,将过期</span>
</div>
<script type="text/javascript">
document.getElementById('date_sel').onchange = function(){
var time = new Date();
var pre_year = time.getFullYear();
var pre_month = time.getMonth();
var pre_day = time.getDate();
var sel_day = parseInt(document.getElementById('date_sel').value);
switch(sel_day){
case 0: pre_day +=1;break;
case 1: pre_day +=2;break;
case 2: pre_day +=3;break;
case 3: pre_day +=4;break;
default:pre_day +=1;break;
}
var endtime = new Date(pre_year, pre_month, pre_day);
document.getElementById('lastdate').innerHTML = endtime.getFullYear()+"-"+(endtime.getMonth()+1)+"-"+endtime.getDate();
};
</script>
</body>
</html>