Js计算上月与当前时间戳的差值


//获得上月时间
initLastMonthTime:function (){


var now = new Date(); //当前日期
var nowYear = now.getFullYear(); //当前年
var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastMonth = lastMonthDate.getMonth();





//获得上月开始时间
var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
var formatMonth=(new Date(lastMonthStartDate).getMonth() + 1)<10? "0"+(new Date(lastMonthStartDate).getMonth() + 1):new Date(lastMonthStartDate).getMonth() + 1;
var formatDate=new Date(lastMonthStartDate).getDate()<10? "0"+new Date(lastMonthStartDate).getDate():new Date(lastMonthStartDate).getDate();

var lastStartTime=new Date(lastMonthStartDate).getFullYear() + '-' + formatMonth + '-' + formatDate ;





//获得上月结束时间
var lastMonthEndDate = new Date(nowYear, lastMonth, dataStatistics.getMonthDays(lastMonth));
var formatEndMonth=(new Date(lastMonthEndDate).getMonth() + 1)<10? "0"+(new Date(lastMonthEndDate).getMonth() + 1):new Date(lastMonthEndDate).getMonth() + 1;
var formatEndDate=new Date(lastMonthEndDate).getDate()<10? "0"+new Date(lastMonthEndDate).getDate():new Date(lastMonthEndDate).getDate();

var lastEndTime=new Date(lastMonthEndDate).getFullYear() + '-' +formatEndMonth + '-' + formatEndDate;


},



//获得某月的天数
getMonthDays:function (lastMonth){

var now = new Date(); //当前日期
var nowYear = now.getYear(); //当前年
var monthStartDate = new Date(nowYear, lastMonth, 1);
var monthEndDate = new Date(nowYear, lastMonth + 1, 1);
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
return days;
},



//进行时间计算
var nowDate=new Date();
var strDateS = new Date(lastStartTime);
var strDateE = new Date(lastEndTime);
var last_start=(nowDate-strDateS)/(1000*3600*24);
var last_end=(nowDate-strDateE)/(1000*3600*24);

猜你喜欢

转载自blog.csdn.net/cafuf/article/details/80281982