日期:2014-05-16 浏览次数:20329 次
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title></title> <style> input { width:200px; } </style> </head> <body> <select id="time"> <option value="0">请选择</option> <option value="1">一周</option> <option value="2">一个月</option> <option value="3">三个月</option> <option value="4">半年</option> <option value="5">一年</option> </select> <input type="text" id="stime" onclick="SelectDate(this,'yyyy-MM-dd hh:mm:ss')" value="开始时间"/> <input type="text" id="etime" onclick="SelectDate(this,'yyyy-MM-dd hh:mm:ss')" value="结束时间"/> <script> function $(el){ return typeof el == 'string' ? document.getElementById(el) : el; } var s = $('stime'); var e = $('etime'); $('time').onchange = function(){ var d = new Date(); e.value = d.toLocaleString().replace(/年|月/g, '-').replace('日', ''); switch( +this.value ){ case 1: s.value = setTime(d, {type:'d', value: -7}); break; case 2: s.value = setTime(d, {type:'M', value: -1}); break; case 3: s.value = setTime(d, {type:'M', value: -3}); break; case 4: s.value = setTime(d, {type:'M', value: -6}); break; case 5: s.value = setTime(d, {type:'y', value: -1}); break; } } function setTime(d, opts){ if(!d) return; var t = opts.type || 'd', v = opts.value || 1; switch( t ){ case 'y': d.setFullYear( d.getFullYear() + v ); break; case 'M': d.setMonth( d.getMonth() + v ); break;