The JavaScript date object can be used to get year, month and day. You can display a timer on the webpage by the help of JavaScript date object.
You can use different Date constructors to create date object. It provides methods to get and set day, month, year, hour, minute and seconds.
The Date object lets you work with dates (years, months, days, minutes, seconds, milliseconds)
The Date object is a datatype built into the JavaScript language.
The Date object lets us work with dates.
A date consists of a year, a month, a day, a minute, a second, and a millisecond.
Date objects are created with the new Date() constructor.
There are 4 ways of initiating a date:
new Date( ) new Date(milliseconds) new Date(datestring) new Date(year,month,date[,hour,minute,second,millisecond ])
Note: Paramters in the brackets are always optional
Here is the description of the parameters:
Using new Date(), creates a new date object with the current date and time:
Current Date & Time: <span id="demo"></span> <script type="text/javascript"> var display = new Date(); // Date Object document.getElementById('demo').innerHTML = display; </script>
Current Date & Time:
Using new Date(date string), creates a new date object from the specified date and time:
Using String Date & Time: <span id="demo"></span> <script type="text/javascript"> var display = new Date("January 11, 2015 12:10:00"); document.getElementById('demo').innerHTML = display; </script>
Using String Date & Time:
Using new Date(number), creates a new date object as zero time plus the number.
Using Number Date & Time: <span id="demo"></span> <script type="text/javascript"> var display = new Date(999986999900); document.getElementById('demo').innerHTML = display; </script>
Using Number Date & Time:
Using new Date(7 agruments), creates a new date object with the specified date and time:
var x = new Date(year,month,date,hour,minute,second,millisecond); // Date Object
Using 7 agruments Date & Time: <span id="demo"></span> <script type="text/javascript"> var display = new Date(2015,7,11,12,13,30,0); document.getElementById('demo').innerHTML = display; </script>
Using 7 agruments Date & Time:
The toUTCString() method converts a date to a UTC string (a date display standard).
Current Date & Time: <span id="demo"></span> <script type="text/javascript"> var display = new Date(); document.getElementById('demo').innerHTML = display.toUTCString(); </script>
Current Date & Time:
The toDateString() method converts a date to a more readable format.
Current Date & Time: <span id="demo"></span> <script type="text/javascript"> var display = new Date(); document.getElementById('demo').innerHTML = display.toDateString(); </script>
Current Date & Time:
The important methods of date object.
Method | Description |
---|---|
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 Time: <span id="hms"></span> <script type="text/javascript"> var dispaly = new Date(); var h = dispaly.getHours(); var m = dispaly.getMinutes(); var s = dispaly.getSeconds(); document.getElementById('hms').innerHTML= h+":"+m+":"+s; </script>
Current Time:
There are two ways to set interval in JavaScript: by setTimeout() or setInterval() method.
Current Time: <span id="view"></span> <script type="text/javascript"> window.onload = function(){getTime();} function getTime(){ var display = new Date(); var h = display.getHours(); var m = display.getMinutes(); var s = display.getSeconds(); // add a zero in front of numbers<10 m = checkTime(m); s = checkTime(s); document.getElementById('view').innerHTML=h+":"+m+":"+s; setTimeout(function(){getTime()},1000); } //setInterval("getTime()",1000);//another way function checkTime(i){ if (i<10){ i="0" + i; } return i; } </script>
Current Time: