js获取当前时间。格式为2016-03-23.
var d = new Date(), mon = parseInt(d.getMonth()+1), str = ""; if(mon<10){ str = d.getFullYear()+"-0"+(d.getMonth()+1)+"-"+d.getDate(); }else{ str = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate(); } console.log(str)
获取当前时间,特定时间前的日期
//日期时间计算 function getBeforeDate(n) { var n = n; var d = new Date(); var year = d.getFullYear(); var mon = d.getMonth() + 1; var day = d.getDate(); if(day <= n) { if(mon > 1) { mon = mon - 1; } else { year = year - 1; mon = 12; } } d.setDate(d.getDate() - n); year = d.getFullYear(); mon = d.getMonth() + 1; day = d.getDate(); s = year + "-" + (mon < 10 ? (‘0‘ + mon) : mon) + "-" + (day < 10 ? (‘0‘ + day) : day); return s; } console.log(getBeforeDate(1));//昨天的日期 console.log(getBeforeDate(7));//前七天的日期
时间: 2024-10-16 15:08:36