脚本每月添加相同的数字加上月底的另一个数字

时间:2016-04-18 21:26:26

标签: javascript

我正在开发一个脚本,每年从零开始每天增加3+,并在月末添加+37。此外,它还必须考虑到闰年。这就是我到目前为止......

// Calculate new members since Jan 1st 2016
var initialDate = new Date(2016, 0, 1); // Jan 1st 2016
var now = Date.now();
var difference = 3 * (now - initialDate);
var millisecondsPerDay = 24 * 60 * 60 * 1000;
var daysSince = Math.floor(difference / millisecondsPerDay);

// Write result to HTML
document.getElementById('members_since').innerHTML = daysSince;

我是一个Javascript,我不知道如何在每个月末考虑闰年和37。任何指导都将非常感谢。

1 个答案:

答案 0 :(得分:0)

有许多重复计算日期差异,但也许你想要这样的东西?

var aDay = 24*60*60*1000, 
      today = new Date(), 
      jan_first = new Date(today.getFullYear(),0,1,12,0,0,0).getTime();
today.setHours(12,0,0,0);
var endOfThisMonth = new Date(today.getFullYear(),
      today.getMonth()+1,0,12,0,0),
      diff=today.getTime()-jan_first,
      total=Math.round(diff/aDay)*3;
total+=today.getMonth()*37;
if (today.getTime()==endOfThisMonth) total+=37;