Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - 如何克隆一个Date对象?(How to clone a Date object?)

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

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

1 Reply

0 votes
by (71.8m points)

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中有效)。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...