JavaScript日期setFullYear()方法 (JavaScript Date setFullYear() method)
setFullYear() method is a Date class method, it is used to set the year (in 4 digits) to the Date object with a valid year value (between 1000 to 9999).
setFullYear()方法是Date类的一种方法,用于将年份(4位数字)设置为具有有效年份值(1000到9999之间)的Date对象。
Syntax:
句法:
var dt = new Date();
dt.setFullYear(year);
Examples:
例子:
Input/Date class object declaration:
var dt = new Date();
Function call to set the year:
dt.setFullYear(2022);
Function call to get the year:
dt.getFullYear();
Output:
2022
JavaScript code to demonstrate an example of Date.setFullYear() method
JavaScript代码演示Date.setFullYear()方法的示例
<html>
<head><title>JavaScipt Example</title></head>
<body>
<script>
var dt = new Date();
//getting current year
var year = dt.getFullYear();
//printing
document.write("year: " + year + "<br>");
//setting year to 2022
dt.setFullYear(2022);
//getting & printing year again
year = dt.getFullYear();
document.write("year: " + year + "<br>");
</script>
</body>
</html>
Output
输出量
year: 2019
year: 2022
Reference: JavaScript setFullYear() Method
参考: JavaScript setFullYear()方法
翻译自: https://www.includehelp.com/code-snippets/date-setFullYear-method-with-example-in-javascript.aspx