pbootcms中可用的自动计算日期的代码
<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>
扫码添加技术【解决问题】
专注企业网站建设、网站安全16年。
承接:企业网站建设、网站修改、网站改版、BUG修复、问题处理、二次开发、PSD转HTML、网站被黑、网站漏洞修复等。
专业解决各种疑难杂症,您有任何网站问题都可联系我们技术人员。
本文来自博客园,作者:黄文Rex,转载请注明原文链接:https://www.cnblogs.com/hwrex/p/18439446