快捷日期實現方法:javascript
1.時間戳(timestamp)
java
<script type="text/javascript"> $(document).ready(function() { getTime(dataChange('lastYear1')); generateReport(); }); function getTime(now){ $("#c10").val(now.beginTime); $("#c11").val(now.endTime); } function dataChange(part) { var beginTime; var now = new Date(); var timestamp = Date.parse(now); var month = now.getMonth(); var year = now.getFullYear(); var day = now.getDate(); switch (part) { case 'lastMonth1': now.endTime = timeStamp2String(now); now.beginTime = timeStamp2String(timestamp-30*24*60*60*1000); break; case 'lastMonth3': now.endTime = timeStamp2String(now); now.beginTime = timeStamp2String(timestamp-30*3*24*60*60*1000); break; case 'lastYear1': unsetTime(now); now.endTime = timeStamp2String(now); now.setFullYear(year - 1, month, day); now.beginTime = timeStamp2String(now); break; case 'lastYear2': unsetTime(now); now.endTime = timeStamp2String(now); now.setFullYear(year - 2, month, day); now.beginTime = timeStamp2String(now); break; default: now.endTime = null; now.beginTime = null; break; } function unsetTime(thisdate) { thisdate.setUTCHours(0); thisdate.setUTCMinutes(0); thisdate.setUTCSeconds(0); } return now; } function timeStamp2String(time) { var datetime = new Date(); datetime.setTime(time); var year = datetime.getFullYear(); var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1; var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate(); var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours(); var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes(); var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds(); return year + "-" + month + "-" + date; } <script>