JavaScript中的date.getYear()方法用於根據通用時間獲取指定日期中的年份。從getYear()方法獲得的年份是當前年份減去1900。
用法:
date.getYear()
在此,日期是Date()類的對象。
參數:該函數不接受任何參數。
返回值:此方法返回一個值,該值從1900減去當前年份。
示例1:本示例根據通用時間返回指定日期的年份。
<script>
// "date" is object of Date() class
var date = new Date();
// Use "getYear()" method and stroes
// the returned value to "n"
var n = date.getYear();
// Display the result
document.write("The value returned by getYear() method: " + n)
</script>
輸出:
The value returned by getYear() method: 119
示例2:本示例僅將年份發送給構造函數。
<script>
// Year 2000 is assign to the
// constructor of Date() class
var date = new Date('2000')
// Use "getYear()" method and stroes
// the returned value to "n"
var n = date.getYear();
// Display the returned value
// The value is the subtraction of
// 2000 and 1900
document.write("The value returned by getYear() method: " + n)
</script>
輸出:
The value returned by getYear() method: 100
示例3:在這裏,我們將完整的日期傳遞給構造函數,該日期的格式為month-day-year-time,例如2010年10月15日02:05:32。
<script>
// Full date is assign to the constructor of Date() class
var date = new Date('October 15, 2010, 02:05:32')
// Use "getYear()" method and stroes the
// returned value in "n"
var n = date.getYear();
// Display the returned value
// The value is the subtraction of 2010 and 1900
document.write("The value returned by getYear() method: " + n)
</script>
輸出:
The value returned by getYear() method: 110
相關用法
- Javascript Math.pow( )用法及代碼示例
- Javascript Array some()用法及代碼示例
- Javascript Number()用法及代碼示例
- Javascript Symbol.for()用法及代碼示例
- Javascript toExponential()用法及代碼示例
- Javascript toString()用法及代碼示例
注:本文由純淨天空篩選整理自SoumikMondal大神的英文原創作品 JavaScript | date.getYear() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。