© Russ Ferguson and Keith Cirkel 2017

Russ Ferguson and Keith Cirkel, JavaScript Recipes, 10.1007/978-1-4302-6107-0_6

6. Dates and Times

Russ Ferguson and Keith Cirkel2

(1)Ocean, New Jersey, USA

(2)London, UK

Using the Date Object

Problem

You need to get date and time information.

Solution

You need to create an instance of the Date object. Depending on if you need the current time or some other time in the past or future, you may need to pass parameters to the constructor .

The Code

Listing 6-1. Creating Instances of the Date Object
var now = new Date(); //returns todays date and timeconsole.log(now);var fluxCapacitorDate = new Date('November 5, 1955');console.log(fluxCapacitorDate); //returns the date with time being all 0’ meaning midnight of that ...

Get JavaScript Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.