①时间格式化(将时间转化为“年-月-日”)
function dateFormatConversion(inputTime) {
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d;
};
②计算日期差,返回相差的天数
function getDaysBetween(dateString1,dateString2){
var startDate = Date.parse(dateString1);
var endDate = Date.parse(dateString2);
var days=(endDate - startDate)/(1*24*60*60*1000);
return days;
}
③计算某年某月有多少天,返回日期天数
function getLastDayOfMonth(year,month){
var isrun = year % 400 == 0 || (year % 4 == 0 & year % 100 != 0);
var d=30;
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
d = 31; //大月31号
} else if(month == 2) { //二月
d = isrun ? 29 : 28; //闰年29号,平年28号
}
return d;
}
④获取日期30天前的日期
function getDate(nowDate){
//const nowDate = new Date()
var date = {
year: nowDate.getFullYear(),
month: nowDate.getMonth() + 1,
date: nowDate.getDate(),
}
var newmonth = date.month>10?date.month:''+date.month
var day = date.date>10?date.date:''+date.date
return date.year + '-' + newmonth + '-' + day
}
⑤获取两个日期之间的所有日期
function getdiffdate(start, end){
var result = [];
//使用传入参数的时间
var startTime = new Date(start);
var endTime = new Date(end);
while (endTime - startTime >= 0) {
let year = startTime.getFullYear();
let month = startTime.getMonth();
month = month<9?'0'+(month+1):month+1;
let day = startTime.getDate().toString().length == 1 ? "0" + startTime.getDate() : startTime.getDate();
//加入数组
result.push(year + "-" + month + "-" + day);
//更新日期
startTime.setDate(startTime.getDate() + 1);
}
return result;
}
————————————————————————待补充————————————————