简体   繁体   English

Javascript日期比较不符合预期

[英]Javascript Date Comparison not behaving as expected

I am getting a SQL date - NOT datetime - object pushed into my Javascript code, and I need to see whether it's before today or not. 我正在将一个SQL日期-不是datetime-对象推送到我的Javascript代码中,我需要查看它是否在今天之前。 Here is the code I have (the relevant part): 这是我的代码(相关部分):

todaysDate = new Date();
todaysDate.setHours(0,0,0,0);
var date = Date.parse(row[3]);
// date.setHours(0,0,0,0);
if (date < todaysDate) {
    alert("date is before today");
    dueDate = '<small class="text-danger">';
} else {
    alert("date is after today");
    dueDate = '<small class="text-muted">';
}

row[3] is the source of the SQL date. row [3]是SQL日期的来源。 So, this works fine for everything except dates that are today. 因此,这对除今天以外的所有日期都适用。 Without the commented line, it thinks that anything with today's date is in the past. 没有注释行,它认为具有今天日期的任何内容都是过去的。 With the commented line, my code breaks. 用注释行,我的代码中断了。 Any thoughts as to how to fix this? 关于如何解决这个问题有什么想法吗? Not sure what I'm doing wrong. 不知道我在做什么错。

Thanks! 谢谢!

If your date string is like "2016-04-10" and your time zone is west of GMT, say -04:00, then in browsers compliant with ECMAScript 2016 you will get a Date for "2016-04-09T19:00:00-0400". 如果您的日期字符串类似于“ 2016-04-10”,并且时区位于格林尼治标准时间以西,例如-04:00,那么在符合ECMAScript 2016的浏览器中,您将获得“ 2016-04-09T19:00”的日期: 00-0400" 。

When you create a Date using new Date() and set the hours to zero (assuming it's 10 April where you are), you'll get a Date for "2016-04-10T00:00:00-0400". 当您使用new Date()创建日期并将小时设置为零(假设您在4月10日所在的位置)时,将获得“ 2016-04-10T00:00:00-0400”的日期。

So when compared they have different time values. 因此,比较时它们具有不同的时间值。

What you need is to either treat the string you get from the database as local, or get the UCT date where you are, so: 您需要将从数据库中获得的字符串视为本地字符串,或者将您所在的UCT日期视为当前日期,因此:

 var dateString = '2016-04-10'; var parsedDate = new Date(dateString); var todayUTCDate = new Date(); todayUTCDate.setUTCHours(0,0,0,0); document.write(parsedDate + '<br>' + todayUTCDate); 

But not all browsers parse strings according to ECMAScript 2015 so they should always be manually parsed. 并非所有浏览器都根据ECMAScript 2015解析字符串,因此应始终手动对其进行解析。 Use a library, or write a small function, eg 使用一个库,或编写一个小函数,例如

// Parse date string in format 'yyyy-mm-dd' as local date
function parseISOLocal(s) {
  var b = s.split(/\D/);
  return new Date(b[0], b[1]-1, b[2]);
}

and replace: 并替换:

var date = Date.parse(row[3]);

with: 有:

var date = parseISOLocal(row[3]);

and then in the comparison, compare the time values: 然后在比较中比较时间值:

if (+date < +todaysDate) {

or 要么

if (date.getTime() < todaysDate.getTime()) {

Use getTime() of date object. 使用日期对象的getTime()

The getTime() method returns the number of milliseconds between midnight of January 1, 1970 and the specified date. getTime()方法返回1970年1月1日午夜到指定日期之间的毫秒数。

You can compare miliseconds and do your operations 您可以比较毫秒并进行操作

date.getTime() > todaysDate.getTime()

Also be sure that Date.parse is returning a valid date. 另外,请确保Date.parse返回有效日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM