JavaScript Tutorial – Date Method

JavaScript date methods let you get and set date values.

getFullYear() Method

getFullYear() returns the year of the date in 4 digits.

How to get year of a date in JavaScript

<script type="text/javascript">
   var d = new Date();
   document.write(d.getFullYear());
</script>
Try it yourself »

getDay() Method

getDay() returns the weekday as a number (0-6). In JavaScript week starts with Sunday, which is 0 and ends with Saturday (1).

How to get the day of the week in JavaScript

<script type="text/javascript">
   var d = new Date();
   document.write(d.getDay());
</script>
Try it yourself »

setFullYear() Method

setFullYear() sets a date object to a specific date.

<script type="text/javascript">
   var d = new Date();
   d.setFullYear(2013, 11, 25);
</script>
Try it yourself »

Month and date parameters are optional. So If you only want to set the year, use the following:

<script type="text/javascript">
   var d = new Date();
   d.setFullYear(2013);
</script>

setDate() Method

setDate() sets the day of the month.

<script type="text/javascript">
   var d = new Date();
   d.setDate(25);
</script>
Try it yourself »