获取当前时间方法
// 获取当前日期,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; if (month >= 1 && month <= 9) month = "0" + month; var day = date.getDate(); if (day >= 0 && day <= 9) day = "0" + day; return year + "-" + month + "-" + day; }
其他:
// 获取当前日期加减天数 function getDateStr(addDay) { var date = new Date(); date.setDate(date.getDate() + addDay); var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); return year + "-" + month + "-" + day; }
// 时间转字符串,转成yyyy-MM-dd HH:mm:SS格式 function dateToStr(datetime) { var dateTime = new Date(datetime); var year = dateTime.getFullYear(); var month = dateTime.getMonth() + 1; var date = dateTime.getDate(); var hour = dateTime.getHours(); var minutes = dateTime.getMinutes(); var second = dateTime.getSeconds(); if (month < 10) { month = "0" + month; } if (date < 10) { date = "0" + date; } if (hour < 10) { hour = "0" + hour; } if (minutes < 10) { minutes = "0" + minutes; } if (second < 10) { second = "0" + second; } return year + "-" + month + "-" + date + " " + hour + ":" + minutes + ":" + second; }