赞
踩
- function getLastMonth() {
- var year,lastMonth;
- var date = new Date();
- var nowYear = date.getFullYear(); //当前年:四位数字
- var nowMonth = date.getMonth(); //当前月:0-11
- if (nowMonth == 0) { //如果是0,则说明是1月份,上一个月就是去年的12月
- year = nowYear - 1;
- lastMonth = 12;
- }else { //不是1月份,年份为当前年,月份本来是要减1的,但是由于`getMonth()`的月份本身就是少了1的,所以月份不用变。
- year = nowYear;
- lastMonth = nowMonth;
- }
- lastMonth = lastMonth < 10 ? ('0' + lastMonth) : lastMonth; //月份格式化:月份小于10则追加个0
- let lastYearMonth = year + '-' + lastMonth;
- return lastYearMonth;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- function getLastMonthFirstDay() {
- date = new Date();
- date.setDate(0);
- var y = date.getFullYear(); //获取年份
- var m = date.getMonth() + 1; //获取月份
- m = m < 10 ? "0" + m : m; //月份补 0
- return [y, m, '01'].join("-");
- }
- function getLastMonthLastDay() {
- date = new Date();
- date.setDate(0);
- var y = date.getFullYear(); //获取年份
- var m = date.getMonth() + 1; //获取月份
- var d = new Date(y, m, 0).getDate(); //获取当月最后一日
- m = m < 10 ? "0" + m : m; //月份补 0
- d = d < 10 ? "0" + d : d; //日数补 0
- return [y, m, d].join("-");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。