Animations

API

Arrays

Async

Basics

Challenges

Classes

Console

Dates

Debugging

DOM Elements

DOM Methods

DOM Navigation

DOM Properties

Event Listeners

Flow Control

Forms

Functions

Global Functions

JSON

Keywords

Libraries (3rd party)

Math

Modules

Objects

Snippets

String

Types

Widgets

Window Object

JavaScript - New Date

JavaScript dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC). One day contains 86,400,000 millisecond. The Date object lets us work with dates. A date consists of a year, a month, a day, an hour, a minute, a second, and milliseconds.


// static method
document.getElementById("demo").innerHTML = Date();

Date objects are created with the new Date() constructor. There are 4 ways of initiating a date:

  • new Date();
  • new Date(milliseconds);
  • new Date(dateString);
  • new Date(year, month, day, hours, minutes, seconds, milliseconds);

new Date()
Using new Date(), creates a new date object with the current date and time.


// var d = new Date();
// document.write(d);
Thu Nov 30 2017 07:22:49 GMT-0500 (Eastern Standard Time)

new Date([string])
Using new Date(date string), creates a new date object from the specified date and time.


var d = new Date("October 13, 2014 11:13:00");
document.getElementById("demo").innerHTML = d;
Mon Oct 13 2014 11:13:00 GMT-0400 (Eastern Daylight Time)

new Date([number])
Using new Date(number), creates a new date object as zero time plus the number. Zero time is 01 January 1970 00:00:00 UTC. The number is specified in milliseconds:

    
var d = new Date(86400000);
document.getElementById("demo").innerHTML = d;
Thu Jan 01 1970 19:00:00 GMT-0500 (Eastern Standard Time)
        
        

new Date([7 numbers])
Using new Date(7 numbers), creates a new date object with the specified date and time. The 7 numbers specify the year, month, day, hour, minute, second, and millisecond, in that order. Only the first 3 are required.


// year, month, day, hour (optional), minute (optional), second (optional), millisecond (optional)
var d = new Date(99,5,24,11,33,30,0);
document.getElementById("demo").innerHTML = d;