Home > Web Front-end > JS Tutorial > body text

JavaScript method getFullYear() that returns the year as a four-digit number from a Date object

黄舟
Release: 2017-11-04 14:33:02
Original
2963 people have browsed it

Definition and usage

The getFullYear() method returns a 4-digit number representing the year.

Syntax

dateObject.getFullYear()
Copy after login

Return value

The year returned when the dateObject is represented in local time. The return value is a four-digit number representing the full year including the century value, not the two-digit abbreviation.

Tips and Notes:

Note: This method is always used in conjunction with a Date object.

Example

Example 1

In this example, we will get the current year and output it:

<script type="text/javascript">

var d = new Date()
document.write(d.getFullYear())

</script>
Copy after login

Output:

2017
Copy after login

Example 2

In this example, we will extract the year from the specific date:

<script type="text/javascript">

var born = new Date("July 21, 1983 01:15:00")
document.write("I was born in " + born.getFullYear())

</script>
Copy after login

Output:

I was born in 1983
Copy after login

Example:

<html>
<head>
<title>JavaScript getFullYear Method</title>
</head>
<body>
<script type="text/javascript">
  var dt = new Date("December 25, 1995 23:15:00");
  document.write("getFullYear() : " + dt.getFullYear() ); 
</script>
</body>
</html>
Copy after login

This will produce the following results:

getFullYear() : 1995
Copy after login


The above is the detailed content of JavaScript method getFullYear() that returns the year as a four-digit number from a Date object. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!