我试图在网站上显示服务年限。从本质上讲,我想我可以输入一个开始日期和当前日期,以便在这两个日期之间产生几年的产出。
我可以使用以下代码计算当前年份,但不确定如何计算差异
document.write(2010) + (/d{4}/.exec(Date())[0])
只需从当前年份减去2010
并连接字符串:
document.write((/d{4}/.exec(Date())[0]) - 2010 + ' years of service')
尽管更容易获得年份的方法是使用Date.getFullYear()
:
document.write(new Date().getFullYear() - 2010 + ' years of service')
您可以使用Date
函数提取时间分量。要获得年份,您可以使用以下内容:
today = new Date();
year = today.getFullYear();
计算年份差异(year
值为数字(:
d = year - 2010;
你可以得到这样的
document.write(new Date().getFullYear() - 2010)
document.write(new Date((.getFullYear((-2010(