如何在JavaScript中获取当前年份?
答案 0 :(得分:1503)
创建new Date()
个对象并致电 getFullYear()
:
new Date().getFullYear()
// returns the current year
劫持已接受的答案,提供一些基本的示例上下文,例如始终显示当前年份的页脚:
<footer>
© <span id="year">2018</span>
</footer>
在加载上面的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>
© <span id="year">2018</span> by FooBar
</footer>
答案 1 :(得分:212)
// Return today's date and time
var currentTime = new Date()
// returns the month (from 0 to 11)
var month = currentTime.getMonth() + 1
// returns the day of the month (from 1 to 31)
var day = currentTime.getDate()
// returns the year (four digits)
var year = currentTime.getFullYear()
// write output MM/dd/yyyy
document.write(month + "/" + day + "/" + year)
答案 2 :(得分:82)
这是获取日期的另一种方法
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)
答案 3 :(得分: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 i.e 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);
答案 4 :(得分:1)
这就是我将其嵌入并输出到HTML网页的方式:
<div class="container">
<p class="text-center">Copyright ©
<script>
var CurrentYear = new Date().getFullYear()
document.write(CurrentYear)
</script>
</p>
</div>
HTML页面的输出如下:
版权所有©2018
答案 5 :(得分:1)
以本示例为例,您可以将其放置在任何想要显示的地方,而无需在页脚或其他答案之类的地方引用脚本
jaccard.similarity
在页脚上的版权说明为例
<script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
答案 6 :(得分:1)
在此处找到的大多数答案只有在正确的情况下才是正确的,前提是您需要根据本地计算机的时区和偏移量(客户端)确定当前年份-来源,在大多数情况下,不能认为它是可靠的(因为机器之间可能会有所不同)。
可靠的来源是:
在Date
实例上调用的方法将根据您的计算机的本地时间返回一个值。
更多详细信息可以在“ MDN Web文档”中找到:JavaScript Date object。
为方便起见,我在他们的文档中添加了相关说明:
(...)获取日期和时间或其组成部分的基本方法都在本地(即主机系统)时区和偏移量下工作。
另一个提及此问题的消息来源是:JavaScript date and time object
需要特别注意的是,如果某人的时钟关闭了几个小时或处于不同的时区,那么Date对象将创建与您自己的计算机上创建的时间不同的时间。
您可以使用一些可靠的来源:
但是,如果您根本不关心时间准确性,或者用例需要相对于本地计算机时间的时间值,则可以安全地使用Javascript的Date
基本方法,例如Date.now()
,或者new Date().getFullYear()
(当前年份)。
答案 7 :(得分:1)
实例化类 Date ,并调用其 getFullYear 方法以 yyyy 格式获取当前年份。 像这样:
let currentYear = new Date().getFullYear;
currentYear 变量将保存您要查找的值。
答案 8 :(得分:0)
您可以像这样简单地使用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)