let currentYear = new Date().getFullYear() let currentMonth = new Date().getMonth() let currentDay = new Date().getDate() export default { getAfterYear: function(numberYear) { //获取“多少年”之后的时间 例如 30年后 即 numberYear=30 let afterDate = '' afterDate = new Date(currentYear - numberYear, currentMonth, currentDay) return afterDate }, getAfterDays: function(days) { //获取“多少天数”之后的时间 例如28天后 即days=28 let currentTime = new Date('2022-09-01').getTime() return new Date(Number(currentTime) + 1000 * 60 * 60 * 24 * days) }, isLeapYear: function(year) { var cond1 = year % 4 == 0 //条件1:年份必须要能被4整除 var cond2 = year % 100 != 0 //条件2:年份不能是整百数 var cond3 = year % 400 == 0 //条件3:年份是400的倍数 //当条件1和条件2同时成立时,就肯定是闰年,所以条件1和条件2之间为“与”的关系。 //如果条件1和条件2不能同时成立,但如果条件3能成立,则仍然是闰年。所以条件3与前2项为“或”的关系。 //所以得出判断闰年的表达式: var cond = (cond1 && cond2) || cond3 if (cond) { return 366 } else { return 365 } }, dateDiffer: function(Date_end) { //date1结束时间 let date1 = new Date(Date_end); //date2当前时间 let date2 = new Date('2022-09-01'); date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()); date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()); const diff = date1.getTime() - date2.getTime(); //目标时间减去当前时间 const diffDate = diff / (24 * 60 * 60 * 1000); //计算当前时间与结束时间之间相差天数 return diffDate } }