JS Best Practices

JS References

Javascript Date Object

Learn to manipulate Date and Time using JavaScript Date object

Javascript Date Object

The JavaScript Date object properties and methods are used to extract specific information about date and time, it stores dates as number of milliseconds elapsed since January 1,1970 GMT (Greenwich Mean Time).

JavaScript is a client side language, it is executed on
the Browsers JavaScript Engine hence the Date object returns time and date value which
are local to the browser and not the server.

The Date object can represent dates 285,616 years before and after January 1, 1970

JavaScript Date Object: Creating a Date Object.

A date Object is created by using a new operator
along with the Date constructor

If a Date Constructor is created without any arguments, the date object created gets only the current date and time.

To get date based on another date and time, the millisecond representation of the date is passed as an argument.

The Date.parse() method is used to pass a string argument representing a date, it converts the date string into
a millisecond representation of the date.

Syntax: Javascript Date Object : Constructor.

Example: Javascript Date Object : Constructor.

Give it a TRY! » Note: Refer to Date object reference to see complete list of Date object properties and methods.

Javascript Date Object: Handling Date and Time

The date object stores the date in milliseconds, which is difficult for every day use, hence it needs to be converted to days, hours or minutes.

The below demo calculates the days remaining for new year in milliseconds and then converts them to days.

Example: JavaScript Date Object : Handling Date and Time

Give it a TRY! » Note: Refer to Date object reference to see complete list of Date object properties and methods.

Javascript Date Object: Using prototype Property

The prototype property is used to add more properties and methods to Date object

By making use of Date objects prototype property, you can customize the values of time and date

Example: JavaScript Date Object : Prototype

Give it a TRY! » Note: The new prototype method returns the string value of the current day.