Sunday, 2 October 2016

Javascript Date


The Javascript Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)

Simply for displaying dates its like below:
<script>
document.getElementById("demo").innerHTML = Date();
</script>

The important methods of date object are as follows:

MethodDescription
getFullYear()returns the year in 4 digit e.g. 2015. It is a new method and suggested than getYear() which is now deprecated.
getMonth()returns the month in 2 digit from 1 to 31.
getDate()returns the date in 1 or 2 digit from 1 to 31.
getDay()returns the day of week in 1 digit from 0 to 6.
getHours()returns all the elements having the given name value.
getMinutes()returns all the elements having the given class name.
getSeconds()returns all the elements having the given class name.
getMilliseconds()returns all the elements having the given tag name.

Current date example:
<script> 
var today=new Date();
document.getElementById('txt').innerHTML=today;
</script>

Current Time example:
<script> 
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
</script> 

No comments :

Post a Comment