gpt4 book ai didi

datetime - GAS 函数返回不正确的日期 - 是我还是缺陷?

转载 作者:行者123 更新时间:2023-12-04 02:15:14 28 4
gpt4 key购买 nike

除非我使用 .toLocaleDateString() 方法,否则下面的函数返回的日期比应有的日期少一天。我做错了什么或者这是一个缺陷?

    function myDate() {
var sDate = '05/10/2012';
var parts = sDate.split('/');
var d = new Date( parts[2], parts[1]-1, parts[0]);
Logger.log(d);
Logger.log(d.toLocaleDateString());
};

Logger 返回:

2012 年 10 月 4 日星期四 16:00:00 PDT

2012 年 10 月 5 日 00:00:00 BST

我在英国,因此使用英国日期格式。我检查过我的项目属性是否将时区设置为“(GMT+00:00) London”,那么为什么 Logger 显示 PDT 的第一个日期?这是错误日期的原因吗?我在一个独立项目中重现了这个问题。 Here's a link to it.

我想将字符串变量转换为日期对象以便进行一些日期数学运算,因此必须使用 .toLocaleDateString() 方法转换回字符串没有帮助。

我已经检查了一致性,认为也许我可以通过测试其他日期来解决它。奇怪的是,如果我将 sDate 的值更改为 01/01/2012 和 04/03/2012 之间的任何值,它会准确返回。从 2012 年 5 月 3 日开始,它下降了一天。对于 2013 年的日期,它会正确返回,直到 29/04/2013 再次开始下降一天。所以它不一致。似乎与报告的问题相似 here .

最佳答案

我发现 getValues() 函数在旧 DST 规则和新 DST 规则之间的范围内读取的日期减少了 1 小时。因此,日期“10/30/2012”的隐式时间是 00:00:00。但是,当读入时,它正在递减一个小时,在前一天降落,即 10/29/2012 23:00:00。

这两个时间范围之间的任何日期(不包括开始日但包括结束日)目前都会表现出这种行为,至少在 getValues() 函数读取时是这样:

  • 从 10 月的最后一个星期日到 11 月的第一个星期日
  • 从三月的第二个星期日到四月的第一个星期日。

我最终编写了动态计算当年这些日期的代码,如果日期落在目标范围内,我只需将它增加一个小时。这可能是修复它的漫长方法,但它确实有效。

这是我的代码:

/*
The old rules for DST stated that the time change occurred on the last Sunday in October,
which would be 10/28/2012. So, when you type in 10/29/2012, the timestamp associated with the date (00:00:00) is being decremented by an hour (23:00:00),
which drops it into the previous day. The new rules for DST states that DST ends on the 1st Sunday in November, which is 11/04/2012. Also, the DST rules
for springtime are also an impacted range of dates that exhibit this behavior, between the second sunday in March and the first sunday in April.

Note: Running this function from the script editor does not produce the strange DST behavior. It seems to be an issue with the getValues() function on a Range object.
*/
function fixDateDSTProblem(lstrDate){
var d = new Date(lstrDate);
//Example ranges affected
//10/29/2012 - 11/04/2012
//03/11/2013 - 04/07/2013

if(isNaN(d.getMonth())){
//return what was passed in if it's not a date or null.
return lstrDate;
}
else if(isAffectedDate(d)){
//increment by one hour
return new Date(d.getTime() + (60 * 60 * 1000));
}
else{
//date is not affected, simply return it as it was passed.
return lstrDate;
}
}



//Check to see if a date is within a beginning and end date
function dateWithin(beginDate,endDate,checkDate) {
var b,e,c;
b = Date.parse(beginDate);
e = Date.parse(endDate);
c = Date.parse(checkDate);
//slightly modified this for the date checking since beginning date is not inclusive, so needs to be > instead of >=
if((c <= e && c > b)) {
return true;
}
return false;
}



function isAffectedDate(targetDate){
var yearNum = targetDate.getFullYear();

//Get the last Sunday in October
var lastSunOctDateStr = getLastOccurrenceDate(0, 10, yearNum);

//Get the first Sunday in November
var firstSunNovDateStr = getOccurrenceDate(1, 0, 11, yearNum);

//Get the second Sunday in March
var secondSunMarDateStr = getOccurrenceDate(2, 0, 3, yearNum);

//Get the first Sunday in April
var firstSunAprDateStr = getOccurrenceDate(1, 0, 4, yearNum);


//if the date is between the last sunday in october and the first sunday in november
// or if the date is between the second sunday in march and the first sunday and april, fix it up!
if(dateWithin(lastSunOctDateStr, firstSunNovDateStr, targetDate) ||
dateWithin(secondSunMarDateStr, firstSunAprDateStr, targetDate)){
return true;
}
return false;
}



function getOccurrenceDate(numOccurrence, dayIndex, monthCalendar, yearNum){
//"Get date of first occurrence of Monday in June 2013"
//"Get date of the second occurrence of Sunday in April 2013"
//dayIndex: Sunday = 0, Saturday = 6
var monthIndex = monthCalendar - 1;
var numFirstXDay = null;
var firstDay = new Date(monthCalendar+"/01/"+yearNum);
var numDayOfWeek = firstDay.getDay();
if(numDayOfWeek == dayIndex){
numFirstXDay = 1;
}
else if(numDayOfWeek > dayIndex){
numFirstXDay = 1+(6-numDayOfWeek)+1+dayIndex+(7*(numOccurrence-1));
}
else if(numDayOfWeek < dayIndex){
numFirstXDay = 1+(dayIndex - numDayOfWeek)+(7*(numOccurrence-1));
}

return monthCalendar+"/"+numFirstXDay+"/"+yearNum;
}



function getLastOccurrenceDate(dayIndex, monthCalendar, yearNum){
//Example: "Get date of last occurrence of Monday in June 2013"
var monthIndex = monthCalendar - 1;
var numLastXDay = null;
//TODO: Handle Leap Year!
var monthMaxDaysArray = {
'1':'31',
'2':'28',
'3':'31',
'4':'30',
'5':'31',
'6':'30',
'7':'31',
'8':'31',
'9':'30',
'10':'31',
'11':'30',
'12':'31'}

var lastDay = new Date(monthCalendar + "/"+monthMaxDaysArray[monthCalendar]+"/" + yearNum);
var numDayOfWeek = lastDay.getDay();
if(numDayOfWeek == dayIndex){
numLastXDay = 31;
}
else if(numDayOfWeek > dayIndex){
numLastXDay = monthMaxDaysArray[monthCalendar] - (numDayOfWeek - dayIndex);
}
else if(numDayOfWeek < dayIndex){
numLastXDay = (monthMaxDaysArray[monthCalendar] - numDayOfWeek) - (6 - (dayIndex-1));
}

return monthCalendar + "/" + numLastXDay + "/" + yearNum;
}

关于datetime - GAS 函数返回不正确的日期 - 是我还是缺陷?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12748363/

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