What is JavaScript Date Format?

JavaScript date constructor is a utility that allows developers to represent a single moment in time; they are used to create dynamic dates or store specific dates. The value returned is usually the number of milliseconds since Jan. 1, 1970, UTC– this date is known as the ECMAScript epoch.

Working With JavaScript Date

There are numerous ways to use a JavaScript date object; we’d go through some of them below:

1. Get The Current Date And Time

Getting the current date and time is the most straightforward JavaScript date operation; we do this by creating a new date object using the constructor function. The value returned can be put in a variable and referenced later in the program.

// How to get the current date and time
let currentDate = new Date();
console.log(currentDate);

2. Pass In Custom Date And Time Values

We can pass in the Year, Month, Day, Hour, Minute, Second, and Millisecond with JavaScript dates. You would place the value in the order above as arguments in the new constructor function. Note that the values of the month argument are 0-index based; for example, the value for January is 0, February is 1, etc.

// The statements below will return the specific date and time: Tue Mar 22 2022 09:20:10
let specificDate = new Date(2022, 2, 22, 9, 20, 10, 10);
console.log(specificDate);

3. Create Dates With A Time Value

We can create dates with a time value in JavaScript; to do this, we would input the number of seconds from the ECMAScript epoch as the single argument of the constructor function.

// To return a specific time
let specificTime = new Date(1647471600000);
console.log(specificTime);

4. Convert JavaScript Date To And From String

The result that the date constructor returns looks like a string, but it isn’t; JavaScript date in its original form is an object type. To convert the value returned into a string, you can use the toString() method; here’s how:

let currentDate = new Date();

// returns the current date, time and time zone as a string
console.log(currentDate.toString());

// returns the current date as a string
console.log(currentDate.toDateString()); 

// returns the current time as a string
console.log(currentDate.toTimeString());

// returns the current date in the GMT time zone 
console.log(currentDate.toUTCString());

We can also convert date strings into JavaScript date objects:

let dateObject = new Date("March 17 2022");
console.log(dateObject);

5. Get Aspects Of The Date Object

The good part of having the date constructor return as an object is that you can get specific aspects of the output instead of the entire output. Using specified methods, you can get the year, month, week, hours, and time of a particular date object.

let currentDate = new Date();

// get the full year
console.log(currentDate.getFullYear());

// get the month of the date (0 - 11)
console.log(currentDate.getMonth());

// get the date of the month (1 - 31)
console.log(currentDate.getDate());

// get the day of the week (0 - 6)
console.log(currentDate.getDay());

// get the hour of the date (0 - 23)
console.log(currentDate.getHours());

// get minutes of a date (0 - 59)
console.log(currentDate.getMinutes());

// get seconds of a date (0 - 59)
console.log(currentDate.getSeconds());

// set milliseconds of a date (0 - 999)
console.log(currentDate.getMilliseconds());

// get the amount of milliseconds since 1st January, 1970 (ECMAScript Epoch)
console.log(currentDate.getTime());

6. Set Aspects Of The Date Object

It is possible to set various parts of an existing date object to a specific date and time. It is possible to set the year, month, date, hours, minutes, seconds, and milliseconds.

let currentDate = new Date();

// set the full year of a date
console.log(currentDate.setFullYear(2022));

// set the month of a date (0 - 11)
console.log(currentDate.setMonth(2));

// set the date of a month (1 - 31)
console.log(currentDate.setDate(17));

// set hour of a date (0 - 23)
console.log(currentDate.setHours(13));

// set minutes of a date
console.log(currentDate.setMinutes(23));

// set seconds of a date
console.log(currentDate.setSeconds(14));

// set milliseconds of a date (0 - 999)
console.log(currentDate.setMilliseconds(124));

// set the time to be a given amount of milliseconds from the ECMAScript epoch (Jan. 1, 1970)
currentDate.setTime(1647471600000)

7. Calculate Elapsed Time

We can also use the date object to figure out how much time has elapsed from one date to another. 

In the sample below, we’ll get the date at the particular point and run an anonymous function that takes some time. We would get the current date at the point when the function has finished running; finally, we subtract the start time from the end time. The program would give the result of this subtraction in milliseconds; to convert it to seconds, divide it by 1000.

let start = new Date();
(function () {
	for(i = 0; i < 10000000; i++) {
	}
})();
let end = new Date();

let elapsed = (end.getTime() - start.getTime()) / 1000;
console.log(elapsed)

Read More: What is a JavaScript Map?

Leave a Comment