This tutorial will cover the basics of date object and then explain the different methods and property of date object.
You can get and set the year, month, day, hour, minute, second, and millisecond fields of the date object by using different method. Date object uses either co-ordinated universal time (Universal/GMT) or local time for displaying date information.
Basically Date object is used to work with dates and times. Date objects are created using “new Date ()” syntax. Date () object Accept these listed arguments.
[Var a=new date() ] - without any argument empty Date ( ) constructor creates a Date object set to the current date and time.
[ var a=new date(miliseconds) ]- After entering any numerical value date() object calculates it in millisecond value as returned by the getTime() method .
[ var a=new date(datestring) ] - When single string argument is passed, it is a string representation of a date.
[ var a=new date(year,month,date,hour,minute,second,millisecond) ] - Year: Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1992, rather than 92. Month: month, beginning with 0 for January to 11 for December. Date: day of the month. Hour: hour of the day (24-hour scale). Minute: minute segment of a time reading. Second: second segment of a time reading. Millisecond: Integer value representing the millisecond segment of a time reading.
Let’s take some example to understand the concept of date object. If you are not passing any argument to the date constructer then it will create date object based on user’s system clock.
Listing 1: Script for date object
Figure 1: Above figure is output of Script to start date object.
- getFullYear() - The four digit year (1970-9999)
- getMonth() - Number of month (0-11)
- getDate() - Day of the month (0-31)
- getDay() - Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday
- getHours() - Number of hours (0-23)
- getMinutes() - Number of minutes (0-59)
- getSeconds() - Number of seconds (0-59)
- getTime() - Number of milliseconds
Now instead of displaying the date, Let’s display the format like digital clock .
Listing 2: Script of digital clock
Figure 2: Above figure is showing current time in digital clock format.
It is important to know that if hours or minutes variable is less than 10 then we need to add a zero to the beginning of minutes. This is not necessary, but if it is 3:08 PM then it will display output "3.8 PM".
This tutorial covered almost basic knowledge to deal with date object. Go through all the details and comment here in case of any questions.