JavaScript | Date Objects

08 Jul, 2019
The Date object is an inbuilt datatype of JavaScript language. It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970. But using another methods we can only get and set the year, month, day, hour, minute, second, and millisecond fields using local or UTC or GMT time. So we can represent date and time till the year 275755 using Date object.There are four different way to declare a date, the basic things is that the date objects are created by the new Date() operator.Syntax:
new Date()
new Date(milliseconds)
new Date(dataString)
new Date(year, month, date, hour, minute, second, millisecond)

Example:
new Date():Parameters: The Date() constructor creates a Date object which sets the current date and time depend on the browser’s time zone. It does not accepts any value.Example:<script> // "geeks" is Date objectvar geeks = new Date();   document.write(geeks);// Prints todays date </script>    Output: I will return the current Day Month Date Year Standard Time.Wed Jul 03 2019 19:01:35 GMT+0530 (India Standard Time)

<script> // "geeks" is Date objectvar geeks = new Date();   document.write(geeks);// Prints todays date </script>    
Output: I will return the current Day Month Date Year Standard Time.
Wed Jul 03 2019 19:01:35 GMT+0530 (India Standard Time)

new Date(milliseconds):Parameters: This method accepts single parameter milliseconds which indicates any numeric value. This argument is taken as the internal numeric representation of the date in milliseconds.Example:<script> // "geeks" is Date objectvar geeks = new Date(4500);   document.write("Todays date : " + geeks);</script>    Output:Todays date : Thu Jan 01 1970 05:30:04 GMT+0530 (India Standard Time)

<script> // "geeks" is Date objectvar geeks = new Date(4500);   document.write("Todays date : " + geeks);</script>    
Output:
Todays date : Thu Jan 01 1970 05:30:04 GMT+0530 (India Standard Time)

new Date(datastring):Parameters: This method accepts single parameter dataString which indicates any string value. It is a string representation of a date and return the datastring with the day.Example:<script> // "geeks" is Date objectvar geeks = new Date("October 13, 2013 11:13:00");   document.write("Datastring with day : " + geeks);</script>    Output:Datastring with day : Sun Oct 13 2013 11:13:00 GMT+0530 (India Standard Time)

<script> // "geeks" is Date objectvar geeks = new Date("October 13, 2013 11:13:00");   document.write("Datastring with day : " + geeks);</script>    
Output:
Datastring with day : Sun Oct 13 2013 11:13:00 GMT+0530 (India Standard Time)

l
new Date(year, month, date, hour, minute, second, millisecond):Parameters: This method accepts seven parameters as mentioned above and described below:year: Integer value which represents the year. This should always specify the year in full, i.e. use 2018, instead of use 18.month: Integer value which represents the month. The integer values are starts from 0 for January to 11 for December.date: Integer value which represents the date.hour: Integer value which represents the hour in 24-hours scale.minute: Integer value which represents the minute.second: Integer value which represents the second.millisecond: Integer value which represents the millisecond.Example:<script> // "geeks" is Date objectvar geeks = new Date(2014, 10, 24, 10, 33, 30, 0);   document.write(geeks);</script>                    Output:Mon Nov 24 2014 10:33:30 GMT+0530 (India Standard Time)

year: Integer value which represents the year. This should always specify the year in full, i.e. use 2018, instead of use 18.
month: Integer value which represents the month. The integer values are starts from 0 for January to 11 for December.
date: Integer value which represents the date.
hour: Integer value which represents the hour in 24-hours scale.
minute: Integer value which represents the minute.
second: Integer value which represents the second.
millisecond: Integer value which represents the millisecond.
Example:
<script> // "geeks" is Date objectvar geeks = new Date(2014, 10, 24, 10, 33, 30, 0);   document.write(geeks);</script>                    
Output:
Mon Nov 24 2014 10:33:30 GMT+0530 (India Standard Time)

Properties of Date object:
Prototype: Prototype allows us to add properties and methods to an object.
Date constructor: It defines the function that creates an Date object’s prototype.
Some Methods of Date object: Here are some methods that define the usage of a Date object, These are non-static methods.
Below methods are returns all the values according to local time:
There are many more methods:
Below methods are returns all the values according to universal time:
javascript-date
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to append HTML code to a div using JavaScript ?
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?