Home >Web Front-end >JS Tutorial >JavaScript method getFullYear() that returns the year as a four-digit number from a Date object

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

黄舟
黄舟Original
2017-11-04 14:33:022989browse

Definition and usage

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

Syntax

dateObject.getFullYear()

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>

Output:

2017

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>

Output:

I was born in 1983

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>

This will produce the following results:

getFullYear() : 1995


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!

Statement:
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