JS 获取当前日期的前一天日期(年月日格式)

var time = (new Date).getTime() - 24 * 60 * 60 * 1000;
var yesday = new Date(time); // 获取的是前一天日期
yesday = yesday.getFullYear() + "-" + (yesday.getMonth()> 9 ? (yesday.getMonth() + 1) : "0" + (yesday.getMonth() + 1)) + "-" +(yesday.getDate()> 9 ? (yesday.getDate()) : "0" + (yesday.getDate())); //字符串拼接转格式:例如2018-08-19

yesday.getMonth(); 当yesday 是八月份时 ,该函数返回的月份是7

例:

 

附上常用的Date 对象的方法简介:

·Date   返回当日的日期和时间

·getDate 从 Date 对象返回一个月中的某一天 (1 ~ 31)

·getDay 从 Date 对象返回一周中的某一天 (0 ~ 6)

·getFullYear   根据本地时间获取当前年份(四位数字)

·getHours     根据本地时间获取当前小时数(24小时制,0-23)

·getMilliseconds   根据本地时间获取当前毫秒数(0 ~ 999)

·getMinutes 根据本地时间获取当前分钟数(0 ~ 59)

·getMonth 从 Date 对象返回月份 (0 ~ 11)

·getSeconds    根据本地时间获取当前秒数(0 ~ 59)

·getTime   获取UTC(世界时间)格式的从1970.1.1 0:00以来的毫秒数

转载于:https://www.cnblogs.com/wmyll/p/9505953.html