How To Clone A Date Object In JavaScript?

Clone a Date object in JavaScript

JavaScript is quite diverse and provides many different ways to clone a Date object in JavaScript. In this short article, we will show you how to do that. Read on and practice now.

Clone a Date object in JavaScript

We will show you four ways to clone a Date object in JavaScript:

Using Date Constructor

Date Constructor returns a Date object.

Syntax:

new Date(value);

Parameters:

  • value: if it is a number, it will represent the number of milliseconds since January 1, 1970, 00:00:00. If it is a string, it will represent a date string. Such as MM/DD/YYYY, YYYY/MM/DD, MM-DD-YYYY, or YYYY-MM-DD.

This approach is the simplest way to clone a Date object. Just pass the object you want to clone into the Date constructor. Then, to know if we have cloned successfully or not, compare the old object and the new object with the operator (===). If the result is false, we have cloned successfully.

const today = new Date();
console.log(today.toLocaleString());

// Clone Date
const cloneToday = new Date(today);
console.log(cloneToday.toLocaleString());

// If clone is successful, return false
console.log(today === cloneToday); // expected output: false

Output:

10/20/2022, 11:34:36 AM
10/20/2022, 11:34:36 AM
false

Using getTime() function

The Date.getTime() function returns the number of milliseconds from 00:00:00 on 1/1/1970 to the date of the Date object.

Syntax:

Date.getTime()

The method has no parameters to be passed.

The getTime() function will return the number of milliseconds, and this number is perfectly valid to pass to the Date constructor, so we can also use the getTime() function to clone a Date object. Like this:

const today = new Date();
console.log(today.toLocaleString());

// Using getTime() to clone Date
const cloneToday = new Date(today.getTime());
console.log(cloneToday.toLocaleString());

// If clone is successful, return false
console.log(today === cloneToday); // expected output: false

Output:

10/20/2022, 11:45:34 AM
10/20/2022, 11:45:34 AM
false

Using valueOf() function

The Date.valueOf() function returns the primitive value of an object.

The primitive value of the Date object is the number of milliseconds from 00:00:00 1/1/1970 until the time of the Date object.

Syntax:

Date.valueOf()

The method has no parameters to be passed.

In this case, using the valueOf() function and using the getTime() function are the same. Both return the number of milliseconds from the time 00:00:00 1/1/1970 until the time of the object.

So we can replace getTime() with valueOf().

const today = new Date();
console.log(today.toLocaleString());

// Using valueOf() to clone Date
const cloneToday = new Date(today.valueOf());
console.log(cloneToday.toLocaleString());

// If clone is successful, return false
console.log(today === cloneToday); // expected output: false

Output:

10/20/2022, 11:47:07 AM
10/20/2022, 11:47:07 AM
false

Using the unary plus operator (+)

The unary plus operator can convert negative, hexadecimal, and string values to numbers. Moreover, it can also convert Date objects to timestamps.

Syntax:

+variable

Description:

Unary plus (+) returns the number value of the variable.

In this way, use + in front of the Date object, and we can get the number of milliseconds like the valueOf() and getTime() functions.

const today = new Date();
console.log(today.toLocaleString());

// Using Unary plus operator to clone Date
const cloneToday = new Date(+today);
console.log(cloneToday.toLocaleString());

// If clone is successful, return false
console.log(today === cloneToday); // expected output: false

Output:

10/20/2022, 11:52:18 AM
10/20/2022, 11:52:18 AM
false

Summary

We just showed you four ways to clone a Date object in JavaScript. In our opinion, the first way: use Date Constructor is the most convenient. If you found the article helpful, please share it with everyone.

Have a happy and exciting new day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *