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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
