gpt4 book ai didi

javascript - 为什么 startDate 会随着 currentDate 一起增加?

转载 作者:行者123 更新时间:2023-12-03 04:07:40 24 4
gpt4 key购买 nike

我不明白为什么在下面的脚本中,startDate 随 currentDate 一起更改,但 name1 保持不变。

getWorkdays('01/03/2017', '05/03/2017', 'Jimmy');
function getWorkdays (startDate, endDate, name1) {

var startDate = nlapiStringToDate(startDate);
var endDate = nlapiStringToDate(endDate);
var name1 = name1;

nlapiLogExecution('DEBUG', 'Conversion of String to Date', 'startDate is now ' + startDate + ' and endDate ' + endDate + ' and the name is ' + name1);

var name2 = name1;
var currentDate = startDate;
nlapiLogExecution('DEBUG', '', 'The currentDate is'+ currentDate + ' and the startDate is ' + startDate);

while (currentDate <= endDate) { // Loop through all dates between startDate and endDate

var weekday = currentDate.getDay(); // Retrieve the weekday (in numeric format with sunday = 0) from the currentDate

if (weekday == 1 || weekday == 6){ // Perform the following loop only if weekday is a Saturday or Sunday

nlapiLogExecution('DEBUG', '', 'The weekday number is ' + weekday + ' and the name is ' + name2);

}

var name2 = 'Jose';
currentDate.setDate(currentDate.getDate() + 1); // Go to the next date
nlapiLogExecution('DEBUG', '', 'name2 is '+ name2 + ' and name1 ' + name1);
nlapiLogExecution('DEBUG', '', 'The currentDate is'+ currentDate + ' and the startDate is ' + startDate);
}
}

如何将 startDate 保持在 01/03/2017?

最佳答案

之后

var currentDate = startDate;

currentDatestartDate 变量都指向同一个 Date 对象,因此对一个变量所做的更改也会影响另一个变量(两者都“看到”同一个对象)。

之后

var name2 = 'Jose';

name2 变量指向一个新字符串,但这不会改变 name1 指向的内容。要达到与上面相同的效果,您必须对原始字符串进行更改(这在 JavaScript 中是不可能的,因为字符串是不可变的)。

How can the startDate be kept at 01/03/2017?

您需要创建一个新的 Date 对象并使用该对象初始化 currentDate

var currentDate = nlapiStringToDate(startDate);

关于javascript - 为什么 startDate 会随着 currentDate 一起增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44475306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com