JavaScript Date Constructor Guide With Example Programs

The JavaScript Date constructor is a powerful tool that enables developers to work with dates effectively in JavaScript.

In this guide, we will explore the syntax and usage of the Date constructor, as well as various methods for manipulating and formatting dates.

By understanding the JavaScript Date constructor, you can handle dates and times with confidence, creating dynamic and interactive web applications.

What is JavaScript Date Constructor?

The JavaScript Date constructor is a built-in object in JavaScript that is used to create and manipulate dates and times.

It provides a way to work with dates and perform various operations such as creating new dates, getting and setting specific components of a date (like year, month, day, hour, minute, and second), and performing arithmetic operations on dates.

Syntax

  • new Date()
  • new Date(value)
  • new Date(year, monthIndex, day[, hour[, minute[, second[, millisecond]]]])

Parameter

  • new Date() – No parameters are passed.
  • (value) – This creates a new date object based on the provided value. The value can be a string representing a date, a timestamp in milliseconds, or a combination of date and time components.
  • (year, monthIndex, day[, hour[, minute[, second[, millisecond]]]]) – This creates a new date object with the specified year, month, day, and optionally hour, minute, second, and millisecond values.

Return Value

The JavaScript Date constructor does not have a specific return value. When you call the Date constructor with the new keyword to create a new date object, it returns an instance of the Date object.

Example Program of date JavaScript constructor

1. Displaying the Current Date and Time

let currentDate = new Date();

console.log("Current Date and Time:", currentDate);

This program creates a new Date object called currentDate using the JavaScript Date Constructor without any arguments.

It initializes the object with the current date and time. The console.log() statement then displays the currentDate.

Output:

Current Date and Time:
 Fri Jun 16 2023 10:36:01 GMT+0800 (China Standard Time)

2. Creating a Date Object with a Specific Date

let customDate = new Date(2023, 5, 16);

console.log("Custom Date:", customDate);

This program creates a new Date object called customDate using the JavaScript Date Constructor with specific arguments representing the desired date: 2023 for the year, 5 for the month (June, as months are zero-based), and 16 for the day.

The resulting customDate object represents June 16, 2023. The console.log() statement is used to display the customDate object.

Output:

Custom Date:
 Fri Jun 16 2023 00:00:00 GMT+0800 (China Standard Time)

3. Getting the Day of the Week

let date = new Date();
let daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let dayOfWeek = daysOfWeek[date.getDay()];

console.log("Day of the Week:", dayOfWeek);

This program demonstrates how to get the day of the week from a Date object. It creates a new Date object called date using the JavaScript Date Constructor.

It then uses the getDay() method on the date object to obtain the day of the week as a number (0 represents Sunday, 1 represents Monday, and so on).

The resulting number is used as an index to retrieve the corresponding day name from the daysOfWeek array. The console.log() statement displays the day of the week.

Output:

Day of the Week: Friday

4. Adding Days to a Date

let date = new Date();
let numberOfDaysToAdd = 5;

date.setDate(date.getDate() + numberOfDaysToAdd);

console.log("Date after adding 5 days:", date);

This program demonstrates how to add days to a Date object. It creates a new Date object called date using the JavaScript Date Constructor. It then uses the getDate() method on the date object to obtain the current day of the month.

The setDate() method is used to add a specified number of days (numberOfDaysToAdd) to the current date.

The resulting date object represents the date after adding the specified number of days. The console.log() statement displays the updated date.

Output:

Date after adding 5 days:
 Wed Jun 21 2023 10:38:45 GMT+0800 (China Standard Time)

5. Formatting a Date as a String

let date = new Date();

let formattedDate = date.toLocaleString("en-US", {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric"
});

console.log("Formatted Date:", formattedDate);

This program showcases how to format a Date object as a string. It creates a new Date object called date using the JavaScript Date Constructor.

The toLocaleString() method is then called on the date object, passing “en-US” as the locale and an options object specifying the desired formatting options.

In this case, the options include displaying the weekday, year, month, and day in a long format. The resulting formatted date string is stored in the formattedDate variable, which is then displayed using the console.log() statement.

Output:

Formatted Date: Friday, June 16, 2023

Anyway here are some of the functions you might want to learn and can help you:

Conclusion

In conclusion, here are the key points of this topic JavaScript Date Constructor.

  • The JavaScript Date constructor is a useful tool in JavaScript that allows developers to work with dates effectively.
  • It provides various functionalities for creating, manipulating, and formatting dates.
  • By using the Date constructor, developers can confidently handle dates and times, creating dynamic and interactive web applications.
  • The Date constructor has different syntax variations, depending on the parameters passed.
  • When you use the Date constructor with the new keyword, it returns an instance of the Date object.
  • The examples provided illustrate how to display the current date and time, create a date with specific values, retrieve the day of the week, add days to a date, and format a date as a string.
  • Understanding and utilizing the JavaScript Date constructor empowers developers to work with dates efficiently and accurately in their web applications.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Glay Eliver


Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame
 · View all posts by Glay Eliver →

Leave a Comment