Code has been added to clipboard!

JavaScript Date Objects in Your Site: JavaScript New Date Explained

Reading time 3 min
Published Aug 8, 2017
Updated Oct 2, 2019

JavaScript date objects can display date and time in differing formats, depending on the level of detail you aim to include. Dates are incorporated by using JavaScript get current date and JavaScript current time properties.

This tutorial will help you master JavaScript date object properties and features, and explain how the JavaScript date to string function can transform the content of date objects into strings. When you finish this chapter, we suggest following up with JavaScript date methods tutorial.

JavaScript Date Object: Main Tips

  • JavaScript date object allows you to work with milliseconds, seconds, minutes, hours, days, months, and years.
  • JavaScript date object can display a date in various formats.
  • Date methods allow you to set years, months, days, hours, minutes, etc.
  • If you are getting or setting a date without defining the time zone, the time will be chosen according to the browser's time zone.
  • To learn more about JavaScript get current date and other properties, you should follow-up with the JavaScript Date Methods tutorial.

Creating Date Objects

The Date() object allows you to format the date in various formats. In the example below, you can see four most common ways it is performed in JavaScript:

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

If you want to get current time, create a new Date JavaScript object. If the time is not specified, JavaScript new Date() property takes it from the browser and then displays it:

Example
var d = new Date();

Note: months start at index 0 (January) and ends at 11 (December).

If you are using JavaScript new Date(string), it will create date object with specified time and date:

Example
var d = new Date("January 20, 2016 13:30:00");

Using new Date(number) creates a date with 0 time, which means the value of the date object will be 01 January 1970 00:00:00 UTC, plus the number you entered:

Example
var d = new Date(86400000);

Using JavaScript date object new Date(7 numbers) will create an object that will display the years, months, days, hours, minutes, seconds, and milliseconds of the date:

Example
var d = new Date(99,5,24,11,33,30,0);

The code in the example below allows you to pass any four parameters:

Example
var d = new Date(99,5,24,10);

Note: if you specify the year by only using two numbers, the program will read it as 19xx.

Displaying Dates

Displaying dates in HTML code will automatically convert it to a string:

Example
var d = new Date();

It is identical to using a toString() method:

Example
d = new Date();
document.getElementById("example").innerHTML = d.toString();

toDateString() method converts date to a more readable format than the previous one:

Example
var d = new Date();
document.getElementById("example").innerHTML = d.toDateString();

It is also possible to convert a JavaScript date to string. The toUTCString() function will convert the date object to a UTC string (a date display standard):

Example
var d = new Date();
document.getElementById("test").innerHTML = d.toUTCString();

JavaScript Date Object: Summary

  • JavaScript date object displays the current date and time on your site.
  • You can display either date, time, or both.
  • If the date is not specified, JavaScript date object takes it from the browser and gives current time.
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen