Fri Oct 18 2019 11:35:53 GMT+0530 (IST)
Creating Date Objects
Date objects are created with the new Date() constructor.
There are 4 ways to create a new date object:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(date string)
new Date() creates a new date object with the current date and time.
Date objects are static. The computer time is ticking, but date objects are not.
new Date(year, month, ...)
new Date(year, month,...) creates a new date object with a specified date and time.
7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):
6 numbers specify year, month, day, hour, minute, second.
5 numbers specify year, month, day, hour, and minute.
4 numbers specify year, month, day, and hour.milliseconds.
3 numbers specify year, month, and day.
2 numbers specify year and month.
Month can not be omitted. If only one parameter is supplied, it will be treated as milliseconds.
January is 0. December is 11.
One and two digit years will be interpreted as 19xx:
Zero time is January 01, 1970 00:00:00 UTC.
Now the time is: 1571378753606 milliseconds past January 01, 1970
new Date(milliseconds) creates a new date object as zero time plus milliseconds.
Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the milliseconds.
Note:- One day (24 hours) is 86 400 000 milliseconds.
When a Date object is created, a number of methods allow you to operate on it.
Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.
When you display a date object in HTML, it is automatically converted to a string, with the toString() method.
The toUTCString() method converts a date to a UTC string (a date display standard).
The toDateString() method converts a date to a more readable format.
ISO Date "2015-03-25" (The International Standard)
Short Date "03/25/2015"
Long Date "Mar 25 2015" or "25 Mar 2015"
The other formats are not so well defined and might be browser specific.
Wed Mar 25 2015 05:30:00 GMT+0530 (IST)
ISO 8601 is the international standard for the representation of dates and times.
Example (Complete date)
The computed date will be relative to your time zone.
Depending on your time zone, the result above will vary between March 24 and March 25.
Note:- UTC (Universal Time Coordinated) is the same as GMT (Greenwich Mean Time).
When getting a date, without specifying the time zone, the result is converted to the browser's time zone.
In other words: If a date/time is created in GMT (Greenwich Mean Time), the date/time will be converted to IST (Indian Standard Time) if a user browses from India.
Short dates are written with an "MM/DD/YYYY" syntax like this.
Long dates are most often written with a "MMM DD YYYY" syntax like this.
Month and day can be in any order.
And, month can be written in full (January), or abbreviated (Jan).
Commas are ignored. Names are case insensitive.
Date Input - Parsing Dates
If you have a valid date string, you can use the Date.parse() method to convert it to milliseconds.
Date.parse() returns the number of milliseconds between the date and January 1, 1970.
You can then use the number of milliseconds to convert it to a date object.
These methods can be used for getting information from a date object:
getFullYear() Get the year as a four digit number (yyyy)
getMonth() Get the month as a number (0-11)
getDate() Get the day as a number (1-31)
getHours() Get the hour (0-23)
getMinutes() Get the minute (0-59)
getSeconds() Get the second (0-59)
getMilliseconds() Get the millisecond (0-999)
getTime() Get the time (milliseconds since January 1, 1970)
getDay() Get the weekday as a number (0-6)
Date.now() Get the time. ECMAScript 5.
The getTime() Method
The getTime() method returns the number of milliseconds since January 1, 1970:
The getMonth() Method
The getMonth() method returns the month of a date as a number (0-11).
You can use an array of names, and getMonth() to return the month as a name.
The getDate() Method
The get Date() method returns the day of a date as a number (1-31).
UTC Date Methods
UTC date methods are used for working with UTC dates (Universal Time Zone dates):
getUTCDate() Same as getDate(), but returns the UTC date
getUTCDay() Same as getDay(), but returns the UTC day
getUTCFullYear() Same as getFullYear(), but returns the UTC year
getUTCHours() Same as getHours(), but returns the UTC hour
getUTCMilliseconds() Same as getMilliseconds(), but returns the UTC milliseconds
getUTCMinutes() Same as getMinutes(), but returns the UTC minutes
getUTCMonth() Same as getMonth(), but returns the UTC month
getUTCSeconds() Same as getSeconds(), but returns the UTC seconds
Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object.
Set Date Methods
Set Date methods are used for setting a part of a date:
setDate() Set the day as a number (1-31)
setFullYear() Set the year (optionally month and day)
setHours() Set the hour (0-23)
setMilliseconds() Set the milliseconds (0-999)
setMinutes() Set the minutes (0-59)
setMonth() Set the month (0-11)
setSeconds() Set the seconds (0-59)
setTime() Set the time (milliseconds since January 1, 1970)
The setFullYear() Method
The setFullYear() method sets the year of a date object. In this example to 2020.
The setFullYear() method can optionally set month and day.
The setHours() Method
The setHours() method sets the hours of a date object (0-23):
Dates can easily be compared.
The following example compares today's date with January 14, 2100