×

Html & Html5

it training classes Logo

The World's best Software Development Study Portal

Js Date


It is the type of an object which we can used to get year , month and day. We can display the date, day, month , hour and seconds. We have some ways to create the date using the date constructor which are described as follows:-:-

  • Date()
  • Date(milliseconds)
  • Date(datestring)
  • Date(year, month, day, hours, minutes, seconds, milliseconds)
Let's take an example:-

<html>

<body>

<script>

  function ondate(){

   document.getElementById('demo').innerHTML= Date();

}

  function ontime(){

   document.getElementById('demo').innerHTML= getDate();

}

</script>

   <button type= "button" onclick="ondate()">Click here for Date</button>

   <p id="demo"> </p>

</body>

</html>

Output of this program



Let's take another example:-

<html>

<body>

<script>

   var date=new Date();

  var day=date.getDate();{

   document.write("Date is: "+day); }

</script>

</body>

</html>

This is other way how we create the string directly by using the new keyword.



Some more methods and description below :-

Methods Description
getDate() It returns the integer value between 1 and 31 that represents the day for the specified date on the basis of local time.
getDay() It returns the integer value between 0 and 6 that represents the day of the week on the basis of local time.
getFullYears() It returns the integer value that represents the year on the basis of local time.
getHours() It returns the integer value between 0 and 23 that represents the hours on the basis of local time.
getMilliseconds() It returns the integer value between 0 and 999 that represents the milliseconds on the basis of local time.
getMinutes() It returns the integer value between 0 and 59 that represents the minutes on the basis of local time.
replace() It replaces a given string with the specified replacement.
getMonth() It returns the integer value between 0 and 11 that represents the month on the basis of local time.
setDate() It sets the day value for the specified date on the basis of local time.
setDay() It sets the particular day of the week on the basis of local time.
setMonth() It sets the month value for the specified date on the basis of local time.
setSeconds() It sets the second value for the specified date on the basis of local time.






JS training insitute | Best IT Training classes in Gurgaon | Web Designing Training Institute