var mydateInputbeginDate = document.getElementById("beginDate"); var datebeginDate = new Date(); datebeginDate.setMonth(datebeginDate.getMonth()-1); //下面语句判断,当月份小于10月时,前面位0 var YbeginDate = datebeginDate.getFullYear(); var MbeginDate = datebeginDate.getMonth() + 1; if( MbeginDate < 10){ MbeginDate = '0' + MbeginDate; } var DbeginDate = datebeginDate.getDate(); if( DbeginDate < 10){ DbeginDate = '0' + DbeginDate; } var dateStringbeginDate = YbeginDate + "-" + MbeginDate + "-" + DbeginDate; mydateInputbeginDate.value = dateStringbeginDate; var mydateInput = document.getElementById("endDate"); var date = new Date(); //下面语句判断,当月份小于10月时,前面位0 var Y = date.getFullYear(); var M = date.getMonth() + 1; if( M < 10){ M = '0' + M; } var D = date.getDate(); if( D < 10){ D = '0' + D; } var dateString = Y + "-" + M + "-" + D; mydateInput.value = dateString;
参考
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input type="text" class="form-control input-medium" placeholder="开始时间" name="" id="startTime" /> <input type="text" class="form-control input-medium" placeholder="结束时间" name="" id="endTime" /> </body> <script type="text/javascript"> var mydateInputbeginDate = document.getElementById("startTime"); var datebeginDate = new Date(); datebeginDate.setMonth(datebeginDate.getMonth()-1); //下面语句判断,当月份小于10月时,前面位0 var YbeginDate = datebeginDate.getFullYear(); var MbeginDate = datebeginDate.getMonth() + 1; if( MbeginDate < 10){ MbeginDate = '0' + MbeginDate; } var DbeginDate = datebeginDate.getDate(); if( DbeginDate < 10){ DbeginDate = '0' + DbeginDate; } var dateStringbeginDate = YbeginDate + "-" + MbeginDate + "-" + DbeginDate; mydateInputbeginDate.value = dateStringbeginDate; var mydateInput = document.getElementById("endTime"); var date = new Date(); //下面语句判断,当月份小于10月时,前面位0 var Y = date.getFullYear(); var M = date.getMonth() + 1; if( M < 10){ M = '0' + M; } var D = date.getDate(); if( D < 10){ D = '0' + D; } var dateString = Y + "-" + M + "-" + D; mydateInput.value = dateString; </script> </html>