【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
如何获得JavaScript的当前年份?
#1楼
这是获取日期的另一种方法
new Date().getDate() // Get the day as a number (1-31)
new Date().getDay() // Get the weekday as a number (0-6)
new Date().getFullYear() // Get the four digit year (yyyy)
new Date().getHours() // Get the hour (0-23)
new Date().getMilliseconds() // Get the milliseconds (0-999)
new Date().getMinutes() // Get the minutes (0-59)
new Date().getMonth() // Get the month (0-11)
new Date().getSeconds() // Get the seconds (0-59)
new Date().getTime() // Get the time (milliseconds since January 1, 1970)
#2楼
对于当前年份,我们可以使用Date类中的getFullYear(),但是根据需要可以使用许多功能,某些功能如下:
var now = new Date() console.log("Current Time is: " + now); // getFullYear function will give current year var currentYear = now.getFullYear() console.log("Current year is: " + currentYear); // getYear will give you the years after 1990 ie currentYear-1990 var year = now.getYear() console.log("Current year is: " + year); // getMonth gives the month value but months starts from 0 // add 1 to get actual month value var month = now.getMonth() + 1 console.log("Current month is: " + month); // getDate gives the date value var day = now.getDate() console.log("Today's day is: " + day);
#3楼
这就是我将其嵌入并输出到HTML网页的方式:
<div class="container">
<p class="text-center">Copyright ©
<script>
var CurrentYear = new Date().getFullYear()
document.write(CurrentYear)
</script>
</p>
</div>
输出到HTML页面如下:
版权所有©2018
#4楼
您可以像这样简单地使用javascript。 否则,您可以使用momentJs插件,该插件可在大型应用中提供帮助。
new Date().getDate() // Get the day as a number (1-31)
new Date().getDay() // Get the weekday as a number (0-6)
new Date().getFullYear() // Get the four digit year (yyyy)
new Date().getHours() // Get the hour (0-23)
new Date().getMilliseconds() // Get the milliseconds (0-999)
new Date().getMinutes() // Get the minutes (0-59)
new Date().getMonth() // Get the month (0-11)
new Date().getSeconds() // Get the seconds (0-59)
new Date().getTime() // Get the time (milliseconds since January 1, 1970)
function generate(type,element) { var value = ""; var date = new Date(); switch (type) { case "Date": value = date.getDate(); // Get the day as a number (1-31) break; case "Day": value = date.getDay(); // Get the weekday as a number (0-6) break; case "FullYear": value = date.getFullYear(); // Get the four digit year (yyyy) break; case "Hours": value = date.getHours(); // Get the hour (0-23) break; case "Milliseconds": value = date.getMilliseconds(); // Get the milliseconds (0-999) break; case "Minutes": value = date.getMinutes(); // Get the minutes (0-59) break; case "Month": value = date.getMonth(); // Get the month (0-11) break; case "Seconds": value = date.getSeconds(); // Get the seconds (0-59) break; case "Time": value = date.getTime(); // Get the time (milliseconds since January 1, 1970) break; } $(element).siblings('span').text(value); }
li{ list-style-type: none; padding: 5px; } button{ width: 150px; } span{ margin-left: 100px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li> <button type="button" onclick="generate('Date',this)">Get Date</button> <span></span> </li> <li> <button type="button" onclick="generate('Day',this)">Get Day</button> <span></span> </li> <li> <button type="button" onclick="generate('FullYear',this)">Get Full Year</button> <span></span> </li> <li> <button type="button" onclick="generate('Hours',this)">Get Hours</button> <span></span> </li> <li> <button type="button" onclick="generate('Milliseconds',this)">Get Milliseconds</button> <span></span> </li> <li> <button type="button" onclick="generate('Minutes',this)">Get Minutes</button> <span></span> </li> <li> <button type="button" onclick="generate('Month',this)">Get Month</button> <span></span> </li> <li> <button type="button" onclick="generate('Seconds',this)">Get Seconds</button> <span></span> </li> <li> <button type="button" onclick="generate('Time',this)">Get Time</button> <span></span> </li> </ul>
#5楼
以这个例子为例,您可以将其放置在任何想要显示的地方,而无需在页脚或其他答案中引用脚本
<script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
以页脚上的版权说明为例
Copyright 2010 - <script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3154288