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

Categories

0 votes
409 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...