JavaScript Date Object with Example

Updated on     Kisan Patel

The Date object contains a number representing the date and time, rather than a string representation. Dates are managed through the Date object.

You can also access every aspect of a date: year, month, day of week, time, and so on, using specialized get and set methods.

Create a new Date object, without any parameters, and output its value to the web page.

<script>
   var dtElem = document.getElementById("date");
   var dt = new Date();
   dtElem.innerHTML = "<p>" + dt + "</p>";
</script>
<div id="date"></div>

Date object methods

Method Description
getDate Returns day of the month (0–31)
getDay Returns day of the week (0–6)
getFullYear Returns 4-digit full year
getMonth Returns local month (0–11)
getHours Returns local hour (0–23)
setDate Sets the day of month (1–31)
setFullYear Sets 4-digit full year
setUTCDate Sets the date’s day of month in UTC
setSeconds Sets the seconds (0–59)

Get Current Month

   var dt = new Date();
   var month = dt.getMonth();

Get Current Year

   var dt = new Date();
   var month = dt.getFullYear();

Get Current Time

   var dt = new Date();
   var month = dt.getTime();
<div id="date"></div>
<script>
   var dtElem = document.getElementById("date");
   var dt = new Date();
   var month = dt.getMonth();
   var day = dt.getDate();
   var yr = dt.getFullYear();
   dtElem.innerHTML = "<p>" + month + "/" + day + "/" + yr; // print the "4/12/2014"
</script>

JavaScript

Leave a Reply