获取JavaScript当前年份

Sat*_*000 895 javascript date

如何在JavaScript中获取当前年份?

Jas*_*wig 1624

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

new Date().getFullYear()
// returns the current year
Run Code Online (Sandbox Code Playgroud)

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

<footer>
    &copy; <span id="year"></span>
</footer>
Run Code Online (Sandbox Code Playgroud)

在上面的HTML加载后执行的其他地方:

<script>
    document.getElementById("year").innerHTML = new Date().getFullYear();
</script>
Run Code Online (Sandbox Code Playgroud)

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

  • 有关同一系列中的其他功能,请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Getter (24认同)
  • 要根据通用时间获取年份,请使用 [getUTCFullYear()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCFullYear) (2认同)
  • 如果上面的页脚示例只是:&lt;footer&gt; &lt;span id =“ year”&gt; &lt;/ span&gt; &lt;/ footer&gt; (2认同)

Sou*_*rav 219

// 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)
Run Code Online (Sandbox Code Playgroud)

  • 使用getFullYear()而不是getYear().请参阅http://stackoverflow.com/questions/16296897/whats-the-difference-between-javascripts-getyear-and-getfullyear-functions (3认同)

Par*_*ani 88

这是另一种获取日期的方法

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)
Run Code Online (Sandbox Code Playgroud)


IAm*_*oid 37

只需一行 JS 代码即可获取当前年份。

<p>Copyright <script>document.write(new Date().getFullYear());</script></p>
Run Code Online (Sandbox Code Playgroud)


Jer*_*mer 9

这就是我将它嵌入并输出到我的 HTML 网页的方式:

<div class="container">
    <p class="text-center">Copyright &copy; 
        <script>
            var CurrentYear = new Date().getFullYear()
            document.write(CurrentYear)
        </script>
    </p>
</div>
Run Code Online (Sandbox Code Playgroud)

输出到 HTML 页面如下:

版权所有 © 2018

  • 我不同意。这显然是一个初学者问题,我们不应该向任何人,尤其是那些开始学习基础知识的人传授不好的练习。此外,您的答案没有提供任何新信息,因为接受的答案中已经包含了“new Date().getFullYear()”。 (6认同)
  • 1.) 你说不好的做法,但其他人不同意,因为这是有效的 vanilla Javascript;2.) 我的回答提供了如何在 HTML 页面中实现这一点的新信息,而不仅仅是 console.log()。 (3认同)
  • “好”是主观的。给猫剥皮的方法不止一种,解决编码问题的方法也不止一种。如果解决方案简单且有效,那么在您的拙见中,为什么这不是“好”? (2认同)
  • 好吧,先生,很明显,这不是一个明确的客观标准(实际上是一场公开辩论),因为有很多人不同意你的观点:/sf/ask/56199811/ -document-write-considered-a-bad-practice ...还有这里:/sf/ask/1315243331/ 因此,您因您的主观意见而降低我的声誉是不太好,因为还有其他人不同意你的观点。请记住这里的要点,我只是提供了一种将其打印到 HTML 文件而不是 console.log() 的简单方法。 (2认同)
  • 请参阅前两个链接,您可以在其中看到数百名不同意您的观点的人。还有数百人同意你的观点,所以这不是一个明确的问题。你坚持自己的方式或高速公路是相当封闭的,因为你无法确定是否有人会认为 document.write() 是满足他/她需求的最佳解决方案。 (2认同)

Rup*_*wal 6

对于当前年份,我们可以使用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);
Run Code Online (Sandbox Code Playgroud)


Nis*_*K.R 6

你可以像这样简单地使用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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
li{
  list-style-type: none;
  padding: 5px;
}

button{
  width: 150px;
}

span{
  margin-left: 100px;
}
Run Code Online (Sandbox Code Playgroud)