Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Assigning a Date variable to another one will copy the reference to the same instance.(将Date变量分配给另一个变量会将引用复制到同一实例。)

This means that changing one will change the other.(这意味着更改一个将更改另一个。) How can I actually clone or copy a Date instance?(如何实际克隆或复制Date实例?)   ask by árvízt?r? tük?rfúrógép translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
452 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use the Date object's getTime() method, which returns the number of milliseconds since 1 January 1970 00:00:00 ( epoch time ):(使用Date对象的getTime()方法,该方法返回自1970年1月1日00:00:00( 纪元时间 )以来的毫秒数:)

var date = new Date(); var copiedDate = new Date(date.getTime()); In Safari 4, you can also write:(在Safari 4中,您还可以编写:) 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).((它似乎在IE8中有效)。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...