SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 
 

Displaying Year part from date object


We have seen how to create a JavaScript date object and use it for displaying current date and time. Now we will try to learn how to get year part from the date object by using getYear() function.

getYear()
Note that getYear() function returns years in two digits for year between 1900 to 1999 and it returns three digit year starting from year 2000. So for the year 2000 getYear() function will return 100 and for 2001 it will return 101. For the year 2006 it will return 106. Same way for the year less than 1900 it will return negative number starting from -1 for the year 1899 and 0 for the year 1900.


getFullYear()
getFullYear() function returns year in 4 digits for all the years. So it is better to use getFullYear() function than getYear function.

For our example we can create one date object and then use its instances to get the year part by using getFullYear() function. Here is the demo.



Here is the full code of the above demo.

<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function show_now() {
var dt = new Date("Aug 16, 2005 05:55:00");
//var my_time=dt.getYear(); // coment this line if you want to use getFullYear
var my_time=dt.getFullYear(); // comment this line if you want to use getYear

alert(my_time);
}
</script>
</head>


<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

<input type=button value="Show Time" onclick="show_now();">

</body>
</html>

Further readings
Countdown script displaying days , hours , minutes and seconds left from an event
Displaying changing Clock showing date and time in a web page
Timer function to set time for reminder alert
Timer Resetting the timer function
Recursive timer to trigger a code block after a particular interval
Displaying current date and time alert
Displaying Current Month using getMonth() function
Displaying present Year using getFullYear()
Showing Current Date using getDate() function
Displaying Current Day using getDay() function
Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
Date & Time
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.