Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript获取当前年份(Get the current year in JavaScript)

如何获得JavaScript的当前年份?

  ask by Satch3000 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Create a new Date() object and call getFullYear() :

(创建一个new Date()对象并调用getFullYear() :)

new Date().getFullYear()
// returns the current year

Hijacking the accepted answer to provide some basic example context like a footer that always shows the current year:

(劫持已接受的答案以提供一些基本示例上下文,例如始终显示当前年份的页脚:)

<footer>
    &copy; <span id="year"></span>
</footer>

Somewhere else executed after the HTML above has been loaded:

(加载以上HTML之后执行的其他地方:)

<script>
    document.getElementById("year").innerHTML = new Date().getFullYear();
</script>

 document.getElementById("year").innerHTML = new Date().getFullYear(); 
 footer { text-align: center; font-family: sans-serif; } 
 <footer> &copy; <span id="year">2018</span> by FooBar </footer> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...