681

Assigning a Date variable to another one will copy the reference to the same instance. This means that changing one will change the other.

How can I actually clone or copy a Date instance?

8 Answers 8

1011

Use the Date object's getTime() method, which returns the number of milliseconds since 1 January 1970 00:00:00 UTC (epoch time):

var date = new Date();
var copiedDate = new Date(date.getTime());

In Safari 4, you can also write:

var date = new Date();
var copiedDate = new Date(date);

...but I'm not sure whether this works in other browsers. (It seems to work in IE8).

12
  • 9
    JSON for this snippet? Sounds like these people should get their basics clear... Like mistaking jQuery for JavaScript DOM.
    – Boldewyn
    Jul 7, 2009 at 7:57
  • 22
    Another way to write this nice solution would be to extend the Date prototype: Date.prototype.clone = function() { return new Date(this.getTime()); }; Which you could then use as copiedDate = date.clone();
    – Ryan
    Apr 2, 2010 at 8:24
  • 17
    new Date(date) same as new Date(date.getTime()), because JS will try to call date.valueOf() when it need a number, and date.valueOf() is same as date.getTime(), reference Date.valueOf Object.valueOf Sep 27, 2013 at 7:41
  • 13
    Do not use new Date(date), use new Date(date.getTime() or new Date(date.valueOf) instead since the first way can lead to differences between the dates in at least Firefox and IE (not Chrome). For example using toISOString() on the both dates in Firefox generates "2015-04-21T04:56:42.000Z" and "2015-04-21T04:56:42.337Z".
    – crudh
    Apr 21, 2015 at 4:57
  • 11
    new Date(dateObject) is correct. See description of Date(value) constructor where explicit rules for date object is defined. In the older specs the behavior was to call ToNumber on the argument if it wasn't a string so either way this should work in any browser that implements the behavior correctly.
    – Salman A
    Feb 22, 2019 at 10:27
170

This is the cleanest approach

let date = new Date() 
let copyOfDate = new Date(date.valueOf())

console.log(date);
console.log(copyOfDate);

5
  • 11
    The "valueOf()" method for "Date" objects produces the same result as its "getTime()" method (the number of milliseconds since epoch time). Jul 7, 2009 at 7:32
  • 50
    @Steve: true, but getTime() could "looks" like it only returns the time and not include the date as well hence my reference to "cleanest". Frankly the Date type in Javascript is a bit of disaster zone, it should never have been mutable in the first place. Jul 7, 2009 at 8:34
  • 1
    @AnthonyWJones: Right, I see what you mean. Jul 7, 2009 at 11:11
  • 4
    I agree that .valueOf() is more clear. Sometimes I forget and use .getMilliseconds() b/c to me that sounds like it means mean milliseconds since epoch time.
    – Tom Wayson
    Jan 10, 2012 at 19:05
  • 2
    +1 to Steve Harrison: I was wondering if that was the case, thanks for the clarification.
    – Brian Lacy
    Jul 13, 2012 at 17:25
55

Update for 2021

In one respect the notion of cloning a Date object sounds grander than it really is. As far as I can tell, there’s only one piece of instance data, and that is the stored time. What we’re really doing is making a new object with the same time.

Whatever may have been the case in the past, the new Date() constructor definitely accepts a Date object as a single argument:

const date = new Date();        // With or without an argument.
const date2 = new Date(date);   // Clone original date.

The Specification at https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date at step 4(b) indicates that a Date object is definitely acceptable, and that this is equivalent to new Date(date.valueOf()), as suggested by some of the answers above. As I said, all you’re really doing is making a new Date object with the same time as the other.

You’ll also find that the documentation at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date has been updated to include this.

1
  • 5
    This should be the new accepted answer.
    – meduz'
    Nov 1, 2021 at 11:38
40

var orig = new Date();
var copy = new Date(+orig);

console.log(orig, copy);

5
15

Simplified version:

Date.prototype.clone = function () {
    return new Date(this.getTime());
}
4
  • 98
    thou shalt not mess with built-in objects
    – Pawel
    Aug 5, 2014 at 10:02
  • 4
    Thou shalt not mess with objects thou dost not owneth. You should make a new copy and call it SuperDate or something, withing your scope. Lots of hard to test for bugs are caused by object functionality changing unexpectedly.
    – Ray Foss
    Jan 28, 2016 at 13:29
  • This would work, but for maintainability reasons, This approach would be considered as a code smell. I have written an approach that I usually use in my coding: actuts.wordpress.com/2017/01/10/…
    – Allan Chua
    Jan 10, 2017 at 15:33
  • 1
    Also I don't see this need for trying to add methods to built-ins in the first place. Study functional programming and discover why a good old-fashioned function is actually much more powerful than methods on the object itself. It's also shorter: const cloneDate = d => new Date(d.getTime()). Sep 14, 2017 at 14:14
9

I found out that this simple assignmnent also works:

dateOriginal = new Date();
cloneDate = new Date(dateOriginal);

But I don't know how "safe" it is. Successfully tested in IE7 and Chrome 19.

3
  • 10
    Do not use new Date(date), use new Date(date.getTime() or new Date(date.valueOf) instead since the first way can lead to differences between the dates in at least Firefox and IE (not Chrome). For example using toISOString() on the both dates in Firefox generates "2015-04-21T04:56:42.000Z" and "2015-04-21T04:56:42.337Z".
    – crudh
    Apr 21, 2015 at 4:58
  • 5
    That may have been true on old versions of firefox, but on the latest version, new Date(date) works fine. Try it: let date = '2015-04-21T04:56:42.337Z'; new Date(date).toISOString(). Result is the same as input: "2015-04-21T04:56:42.337Z"
    – TetraDev
    Sep 11, 2020 at 18:59
  • 3
    To anyone reading this nowadays, this works, please ignore old comments stating that .getTime must be used. Dec 1, 2021 at 13:57
1
function cloneMyDate(oldDate){
  var newDate = new Date(this.oldDate);
}

I was passing oldDate to function and generating newDate from this.oldDate, but it was changing this.oldDate also.So i used the above solution and it worked.

-2

For chrome it works:

//Thu Nov 03 2022 11:43:00

**const date = new Date(2022,10,03,11,43,0);

const date2 = new Date(date);**

now changing date2 will have no impact on the date

2
  • Please learn how to use markdown and adjust your answer accordingly. Nov 6, 2022 at 18:12
  • The formatting isn’t the only problem with this answer. It contains something that has already been covered by all previous answers. Nov 7, 2022 at 0:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.