我有這樣的代碼在用戶輸入開始日期,然後它會自動添加結束日期。結束日期爲開始日期的+3天。問題在二月,三月26-31之後,所以輸出錯誤的結束日期。它始終輸出錯誤的結束日期從26-31。我認爲問題是閏年,但我不知道如何解決。請幫幫我。JavaScript的日期(閏年)
這是代碼:
<input type="date" name="start_date" id="start" onKeyUp="date()">
<input type="text" name="end_date" id = "end" onKeyUp="date()" disabled />
<script type = "text/javascript">
function date() {
var startdate = document.getElementById('start');
var enddate = document.getElementById('end');
//conver you selected stuff to a proper date format
var old = new Date(Date.parse(startdate.value));
//take a new date
var newdate=new Date(Date.parse(startdate.value)+24*60*60*1000*3);
// add how many days you want to add i use 3 you can use as many
newdate.setDate(old.getDate()+3);
enddate.value = (newdate.getMonth()+1)+'-'+newdate.getDate()+'-'+newdate.getFullYear();
}
</script>
好吧,先生,我會試試這個。謝謝 – arukiri123 2013-02-18 09:41:56
哦。現在正在工作的先生。謝謝 – arukiri123 2013-02-18 09:43:34