主要代码:
date.setMonth(date.getMonth()-1);
var mydateInput = document.getElementById("startTime"); var date = new Date(); date.setMonth(date.getMonth()-1); var dateString = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()+ " "+ "8:00:00"; mydateInput.value = dateString; var mydateInputs = document.getElementById("endTime"); var dates = new Date(); var dateStrings = dates.getFullYear() + "-" + (dates.getMonth() + 1) + "-" + dates.getDate()+ " "+ "18:00:00"; mydateInputs.value = dateStrings;
demo示例:
<!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 mydateInput = document.getElementById("startTime"); var date = new Date(); date.setMonth(date.getMonth()-1); var dateString = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()+ " "+ "8:00:00"; mydateInput.value = dateString; var mydateInputs = document.getElementById("endTime"); var dates = new Date(); var dateStrings = dates.getFullYear() + "-" + (dates.getMonth() + 1) + "-" + dates.getDate()+ " "+ "18:00:00"; mydateInputs.value = dateStrings; </script> </html>