Javascript Javascript日期变量赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6609574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:28:10  来源:igfitidea点击:

Javascript date variable assignment

javascriptvariablesdate

提问by railOne

var date1 = new Date();  
date1.setFullYear(2011, 6, 1);  

// 2011-07-01, ok  
console.log(date1);

// set date2 the same date as date1  
var date2 = date1;

// ...

// now I'm gonna set a new date for date2  
date2.setFullYear(2011, 9, 8);

// 2011-10-08, ok  
console.log(date2);

// 2011-10-08, wrong, expecting 2011-07-01  
// I didn't assign a new date to date1  
// WHY is date1 changed?  
console.log(date1);

回答by SergeS

Date is object , so it is assigned as reference - simple approach is

日期是对象,因此将其分配为引用-简单的方法是

date2 = new Date( date1 );

回答by davin

Both date variables are just references to the same date object in memory. So you need date2to be a cloneof date1. Change:

两个日期变量都只是对内存中同一个日期对象的引用。所以,你需要date2成为一个克隆date1。改变:

var date2 = date1;

to this:

对此:

var date2 = new Date(date1.getTime());

回答by cwallenpoole

JavaScript uses pass by reference for Dates* (as well as all non-primitives -- var o = {}; var j = o; j.foo = 1; console.log(o.foo); //1. On the other hand, for Numbers, Strings, and Booleans var o = 0; var j = o; j++; console.log(j); // 0), so that is expected behavior.

JavaScript 对 Dates* (以及所有非原始类型 -- var o = {}; var j = o; j.foo = 1; console.log(o.foo); //1。另一方面,对于 Numbers、Strings 和 Booleans var o = 0; var j = o; j++; console.log(j); // 0)使用按引用传递,因此这是预期的行为。

If you need to copy a date you can always

如果您需要复制日期,您可以随时

var date2 = new Date( date1.getTime() );

* Please see comments to understand why this is not entirely correct.

* 请参阅评论以了解为什么这不完全正确。

回答by Guest

date2It's a reference to date1.

date2这是对date1.

To achieve the expected results, do the following:

要达到预期的结果,请执行以下操作:

var date1 = new Date();
date1.setFullYear(2011, 6, 1); 

var date2 = new Date();
date2.setTime(date1.valueOf());

回答by yzorg

Variation of @SergeS's answer, but Date() objects in js coerce to number, so you don't need getTime():

@SergeS 答案的变化,但 js 中的 Date() 对象强制为数字,因此您不需要 getTime():

// general case
var dateValueCopy = new Date(date1);

And restated with OP variable names:

并用 OP 变量名称重述:

var date2 = new Date(date1);

回答by Anadi Kumar

<html lang="en">
<head>
<script>
function getDateDiff(time1, time2) {
var str1= time1.split('/');
var str2= time2.split('/');
var t1 = new Date(str1[2], str1[0]-1, str1[1]);
var t2 = new Date(str2[2], str2[0]-1, str2[1]);

var diffMS = t1 - t2;    
console.log(diffMS + ' ms');

var diffS = diffMS / 1000;    
console.log(diffS + ' ');

var diffM = diffS / 60;
console.log(diffM + ' minutes');

var diffH = diffM / 60;
console.log(diffH + ' hours');

var diffD = diffH / 24;
console.log(diffD + ' days');
alert(diffD);
}

//alert(getDateDiff('10/18/2013','10/14/2013'));
</script>
</head>
<body>
<input type="button" onclick="getDateDiff('10/18/2013','10/14/2013')" value="clickHere()" />
</body>
</html>

回答by Alex K.

You need to create a copy of date1, currently date1and date2refer to the same date object.

您需要创建date1, 当前的副本date1date2引用相同的日期对象。

var date2 = new Date(date1.valueOf());