[英]Date object not catching comparisons in google scripts (javascript)
I have a script that is comparing two dates against each other. 我有一个脚本,可以相互比较两个日期。 But both are coming in differently.
但是两者的进入方式有所不同。 one I converted to a string format Mo/day/year then to a date format.
我将其转换为Mo / day / year的字符串格式,然后转换为日期格式。 The other is taken directly for a google spreadsheet cell already in date format.
另一个则直接用于日期格式的Google电子表格单元格。 But when I run a comparison on them, it doesn't work even if the date falls within the comparison range.
但是,当我对它们进行比较时,即使日期落在比较范围内,它也不起作用。 Here's my code:
这是我的代码:
function formatDate(someDate){
var x = someDate.split("T");
var v = x[0].split("-");
var newDate = new Date();
newDate.setFullYear(v[0],(v[1]-1),v[2]);
//var newDate = v[1]+"/"+ v[2]+"/"+v[0];
return newDate;
}
//calling formatDate within another function
if(startDate != ''){
if(singleCard[j].date != undefined){
var k = singleCard[j].date;
var f = formatDate(k);
var dt = new Date(f);
if(dt >= startDate && dt <= endDate){ //date comparison
//do something here
}
The start/end dates is coming in a format of: 开始/结束日期采用以下格式:
Fri Oct 12 2012 00:00:00 GMT-0700 (PDT)
The singleCard[j].date is coming in as a string: singleCard [j] .date将作为字符串输入:
"2012-10-12T16:57:51.517Z"
and then I use: 然后我用:
var f = formatDate(date) -> var dt = new Date(f)
to process it. 处理它。
I'm not sure what I'm doing wrong here. 我不确定我在做什么错。 Thanks.
谢谢。
When you do 当你做
var newDate = v[1]+"/"+ v[2]+"/"+v[0];
are you sure you used year / month / day in the right order ? 您确定您使用的年/月/日顺序正确吗?
Did you try to log the result of var dt = new Date(f);
您是否尝试记录
var dt = new Date(f);
to see its value as a date object ? 看到它的值作为日期对象?
sorry if my questions seem obvious but since I don't know your local settings for date I can't be sure how you handle that.( I'm in a country where we use dd/mm/yyyy so I'm used to have problems with that ;) 对不起,如果我的问题似乎是显而易见的,但因为我不知道日期本地设置我不能确定你如何处理这个问题。(我在这里我们使用DD / MM / YYYY所以我已经习惯了一个国家有问题;)
EDIT : just saw your comment... shouldn't it be v[0]+"/"+ v[1]+"/"+v[2]
(yyyy/mm/dd) 编辑 :刚看到您的评论...不应该是
v[0]+"/"+ v[1]+"/"+v[2]
(yyyy / mm / dd)
EDIT 2 : I think you have to give hh mm ss parameters as well when you use setFullYear(); 编辑2 :我认为您在使用setFullYear()时也必须给hh mm ss参数;
something like 就像是
newDate.setFullYear(v[0],(v[1]-1),v[2],0,0,0,0);
in order to get it working. 为了使它工作。 It is also necessary (I'd even say mandatory) to set hh mm ss and mSec to 0 since you are using a
<=
and >=
comparison, Equality will probably never be true if you let hours , minutes , seconds and milliseconds free to live their own life. 这也是必要的(我甚至会说强制)设置HH毫米ss和毫秒到0,因为你使用的是
<=
和>=
比较,如果你让小时,分钟,秒和毫秒自由平等可能永远不会是真实的过自己的生活。
Don't forget date objects are ALWAYS complete dates with time, even when you don't show / use them. 不要忘记日期对象总是带有时间的完整日期,即使您不显示/使用它们也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.