Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 9
    JSON for this snippet? Sounds like these people should get their basics clear... Like mistaking jQuery for JavaScript DOM. Commented 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(); Commented Apr 2, 2010 at 8:24
  • 19
    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 Commented 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". Commented 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. Commented Feb 22, 2019 at 10:27