Javascript – extract Year from Date

June 24th, 2011 § Leave a Comment

If you want to extract year from Date in Javascript, try getFullYear() method instead of getYear(), see the output below on Firefox and Chrome…

Here's the code:

<html>
<head>
<script langauge="javascript">
var date1 = new Date();
var browser = navigator.userAgent;
var dateMessage = "I am on "+browser+"\n\nCurrent Date : "+
	date1;
dateMessage += "\ndate1.getYear()>> "+date1.getYear()+
	"\ndate1.getFullYear()>> "+date1.getFullYear();
alert(dateMessage);
</script>
</head>
</html>

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Javascript – extract Year from Date at Varun's Blog.

meta

Follow

Get every new post delivered to your Inbox.