Tutorial by Examples

Use new Date() to generate a new Date object containing the current date and time. Note that Date() called without arguments is equivalent to new Date(Date.now()). Once you have a date object, you can apply any of the several available methods to extract its properties (e.g. getFullYear() to get t...
To create a new Date object use the Date() constructor: with no arguments Date() creates a Date instance containing the current time (up to milliseconds) and date. with one integer argument Date(m) creates a Date instance containing the time and date corresponding to the Epoch time (...
var date1 = new Date(); date1.toJSON(); Returns: "2016-04-14T23:49:08.596Z"
By default, a Date object is created as local time. This is not always desirable, for example when communicating a date between a server and a client that do not reside in the same timezone. In this scenario, one doesn't want to worry about timezones at all until the date needs to be displayed in lo...
Convert to String var date1 = new Date(); date1.toString(); Returns: "Fri Apr 15 2016 07:48:48 GMT-0400 (Eastern Daylight Time)" Convert to Time String var date1 = new Date(); date1.toTimeString(); Returns: "07:48:48 GMT-0400 (Eastern Daylight Time)" Conve...
To increment date objects in Javascript, we can usually do this: var checkoutDate = new Date(); // Thu Jul 21 2016 10:05:13 GMT-0400 (EDT) checkoutDate.setDate( checkoutDate.getDate() + 1 ); console.log(checkoutDate); // Fri Jul 22 2016 10:05:13 GMT-0400 (EDT) It is possible to use setD...
The static method Date.now returns the number of milliseconds that have elapsed since 1 January 1970 00:00:00 UTC. To get the number of milliseconds that have elapsed since that time using an instance of a Date object, use its getTime method. // get milliseconds using static method now of Date co...
Formatting a JavaScript date in modern browsers In modern browsers (*), Date.prototype.toLocaleDateString() allows you to define the formatting of a Date in a convenient manner. It requires the following format : dateObj.toLocaleDateString([locales [, options]]) The locales parameter should be...

Page 1 of 1