<script>

	window.onload = function() {

		function calculateDays() {

		var currentDate = new Date('2020-02-21');

		var nextYearDate = new Date(currentDate.getFullYear() + 5, currentDate.getMonth(), currentDate.getDate());

		document.getElementById('qunian').innerHTML = '' + nextYearDate.toLocaleDateString();

		}

		calculateDays();

	};

</script>

上述代码表示:在日期:2020-02-21的基础上加上 5年,以得到未来五年后的日期;2020-02-21可以替换为其他时间标签或日期!

结果调用标签:<span id="qunian"></span>

<script>

	window.onload = function() {

		function otherFunction() {

		var startDate = new Date("2020-02-21");

		var endDate = new Date("2023-07-25");

                // 计算日期间的毫秒数差值

		var timeDiff = endDate.getTime() – startDate.getTime();

		// 将毫秒数差值转换为天数

		var days = Math.ceil(timeDiff / (1000 * 3600 * 24));

                document.getElementById("tianshu").innerHTML = "" + days;

		}

		otherFunction();

	};

</script>

上面代码表示计算两个日期之间的天数,调用代码:<span id="tianshu"></span>