JavaScript Date Objects.. 30daysofjavascript=> Day 5

Olajide Blessing Niniola
2 min readMar 8, 2021

Have you ever wondered how time zones are displayed? JavaScript Date Object lets us work with dates.

Date object in JavaScript

The time at which this project was worked on was logged to the console. How does this work? By default, JavaScript uses the browser’s time zone and display a date as a full-text string: Sun Mar 07, 2021, 02:26:19 GMT+0100 (West Africa Standard Time).

Dates Objects are created with the new Date() constructor. Dates in JavaScript can be created in for ways:

  • new Date(): new Date() creates a new date object with the current date and time.
  • new Date(year, month, …): new Date(year, month, …) creates a new date object with a specified date and time. 7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order), 6 numbers specify year, month, day, hour, minute, second and so on. The month parameter cannot be omitted, if you supply only one parameter it will be treated as milliseconds.
  • new Date(dateString): new Date(dateString) creates a new date object from a date string.
  • new Date(milliseconds): new Date(milliseconds) creates a new date object as zero time plus milliseconds.
How to crate date objects

Get Date methods

These methods below can be used for getting information from a date object. Check out more get date methods here.

Common get date methods

Set Date Methods

Set Date methods are used for setting a part of a date. Get more references on setDate() methods here.

What to remember?

  • Data Objects are static, The computer time ticks, but date objects are not.
  • JavaScript counts months from 0 to 11. January is 0, February is 1 and December is 11.
  • JavaScript stores date as a number of milliseconds since January 01, 1970, 00:00:00 UTC (Universal Time Coordinated). This explains why you cannot omit month in the new Date(year, month, …)
  • In JavaScript, the first day of the week (0) means “Sunday”, even if some countries in the world consider the first day of the week to be “Monday”.
  • JS date formats, read more here.

--

--