O'Reilly logo

JavaScript Recipes: A Problem-Solution Approach by Keith Cirkel, Russ Ferguson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Russ Ferguson and Keith Cirkel 2017
Russ Ferguson and Keith CirkelJavaScript Recipes10.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

var now = new Date(); //returns todays date and time
console.log(now);
var fluxCapacitorDate = new Date('November 5, 1955');
console.log(fluxCapacitorDate); //returns the date with time being all 0’ meaning midnight of that day.
var infinityWarDate = new Date(2018, 4, 4);  //returns ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required