- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
除非我使用 .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() 函数读取时是这样:
我最终编写了动态计算当年这些日期的代码,如果日期落在目标范围内,我只需将它增加一个小时。这可能是修复它的漫长方法,但它确实有效。
这是我的代码:
/*
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/
我的 slideToggle 在幻灯片切换的底部有点跳动。会不会是因为那里有一个按钮之类的。任何使它更平滑的方法。尝试使用缓动但不是很成功。有什么建议 点击视频设置自己看看 The site $(do
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
概述: 我的 B 对象是一个 100 000 * 5000 的 2 GB 大矩阵 我的 A 对象较小 1000 * 5000 analyse_with_glm <- function(Y) { c
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 6年前关闭。 Improve t
我在从 SQL Server (2008R2) 数据库中的 NVARCHAR 字段检索加密数据时遇到了问题,对于某些记录,我的 C# .NET 应用程序中数据的字符串值与数据库记录中的数据字符串值不同
我从 main.cpp 中包含两个头文件,它们在匿名命名空间中具有以下定义:const string strToken = ("%");使用 g++ 4.9 版编译结果如下: In file incl
我正在测试的代码中出现信任边界冲突。该代码在 session 中添加表单,并且由于违反信任边界而存在缺陷 Inside Struts Action class execute method { Ed
这个问题没有与之相关的实际问题,它更多的是一个好奇的问题,想知道我是否过于字面意思;)。 所以我一直在努力尽可能多地理解 c++ 标准。今天,在深入研究标准时,我注意到了这一点 (ISO/IEC 14
在我的数据库中,我必须做一个Circular Reference(cycle)来获取我想要的数据,我不知道如何重新排序表来获取我想要的数据而不需要循环. 这是我的数据库的模式(或模型)——只有表名是英
我有一个代表一组数字的类。构造函数接受三个参数: startValue 、 endValue 和 stepSize 。 该类负责保存一个列表,其中包含考虑 stepSize 的开始值和结束值之间的所有
如何删除下图中标记的三个间隙? 此代码可在 http://jsfiddle.net/69zj6smo/ 获得- 调整渲染区域的大小以查看通常存在的一些线条。 让我感到困惑的是,我认为我总是创建这样的流
double 的位格式在第一位存储符号。 double的C#哈希算法是高低32位二进制异或。 因此,当您对 double A 及其负数 -A 进行哈希处理时,哈希值的唯一区别在于第一位。 要散列多个字
当我在 Action 中使用重定向时,afterAction 方法(在 controller.php 中)不起作用!我该如何解决这个问题? 注意:我不能使用 beforeAction 因为我在我的 A
毫无疑问,还有其他可能更好的方法可以做到这一点,但我正在努力了解这里发生了什么。 在下面的示例中,coverity 在第四行报告了 FORWARD_NULL 缺陷。 double? foo = nul
我们希望针对 Jenkins 中失败的构建自动在 Jira 中创建缺陷。如果您成功完成了此操作,可以与我分享吗? 最佳答案 您应该能够使用JIRA plugin来做到这一点,以及“JIRA:创建问题”
有人能解释一下为什么 VeraCode 似乎认为使用 name 作为公共(public)属性(property)是一个坏主意,并提出了一个好的缓解措施吗? 代码(JavaScript): var Ba
我认为这是 C++11 标准中的一个(次要)缺陷。在 [dcl.dcl] 中我们有: simple-declaration: decl-specifier-seqopt init-
我做了以下... private static IDbConnectionProvider CreateSqlConnectionProvider(DbConfig dbConfig) { r
我现在在运行我的 cakephp 应用程序时遇到了很多麻烦。 在将 vom lenny 升级为 squeeze(甚至尝试完全重新安装 sqeeze)之后,imagick 的速度非常慢,以至于它只是关闭
我收到来自 Veracode 的信任边界违规。我的代码是 userName= req.getParameter(Constant.USERNAME); session.setAttribute(Con
我是一名优秀的程序员,十分优秀!