比较 Typescript / Angular 4 上的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51177703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:30:56  来源:igfitidea点击:

Comparing dates on Typescript / Angular 4

angulartypescript

提问by sAntd

I am trying to compare dated in typescript/angular 4. In my case, I assigned system date into a variable named 'today' and database date assigned into a variable named 'dateToBeCheckOut'. Then I compared dates using if statement, In my case I expected to get the bookings which having checkout date is less than(before)today's date. But I get unexpected result instead of getting the expected result. I attached my code bellow and expect someone's help for understanding why it happens.

我正在尝试比较typescript/angular 4 中的日期。就我而言,我将系统日期分配给名为“today”的变量,并将数据库日期分配给名为“dateToBeCheckOut”的变量。然后我使用 if 语句比较日期,在我的情况下,我希望得到结帐日期小于(之前)今天日期的预订。但是我得到了意想不到的结果,而不是得到了预期的结果。我在下面附上了我的代码,并希望有人帮助理解它为什么会发生。

Here is the code

这是代码

for(let k = 0; k < this.bookingDetailsArrayRealObject.length; k++){
        let dateCheckOut = this.bookingDetailsArrayRealObject[k].package.chackout;
        let dateToBeCheckOut = new Date(dateCheckOut);
        let today = new Date(Date.parse(Date()));
        //let today_test = new Date();
        if(this.bookingDetailsArrayRealObject[k].status){
          if(dateToBeCheckOut.toDateString() < today.toDateString()){
            this.bookingIdsToBeUpdated.push(this.bookingDetailsArrayRealObject[k]._id);
            window.alert('true');
            console.log(today.toDateString());
            console.log(dateToBeCheckOut.toDateString());
            console.log(this.bookingDetailsArrayRealObject[k]);
          }
        }
      }

booking object from database

来自数据库的预订对象

console result

控制台结果

In console result, the 3rd one is unexpected according to the if statement. Any suggestion would be appreciated.

在控制台结果中,根据 if 语句,第三个是意外的。任何建议将不胜感激。

采纳答案by jcroll

You can compare date objects without casting them as strings. Try:

您可以比较日期对象而无需将它们转换为字符串。尝试:

for(let k = 0; k < this.bookingDetailsArrayRealObject.length; k++){
        let dateCheckOut = this.bookingDetailsArrayRealObject[k].package.chackout;
        let dateToBeCheckOut = new Date(dateCheckOut);
        let today = new Date();
        //let today_test = new Date();
        if(this.bookingDetailsArrayRealObject[k].status){
          if(dateToBeCheckOut < today){
            this.bookingIdsToBeUpdated.push(this.bookingDetailsArrayRealObject[k]._id);
            window.alert('true');
            console.log(today.toDateString());
            console.log(dateToBeCheckOut.toDateString());
            console.log(this.bookingDetailsArrayRealObject[k]);
          }
        }
      }

回答by JanRecker

Just a guess, did you check the timezone of the date objects at runtime?

只是猜测,您是否在运行时检查了日期对象的时区?

When i had to handle local (browser) date/time and database (server) date/times i very often had the problem of different timezones.

当我不得不处理本地(浏览器)日期/时间和数据库(服务器)日期/时间时,我经常遇到不同时区的问题。

An example: The database sends a date (in UTC), in the format "20190621T00:00:00" (ISO-8601 conform but without timezone information) I transfered that information with Date("20190621T00:00:00") into a Date object.

一个例子:数据库发送一个日期(UTC),格式为“20190621T00:00:00”(符合ISO-8601但没有时区信息)我用Date(“20190621T00:00:00”)将该信息传输到一个日期对象。

Now the fun started, because my computer was NOT in the UTC Timezone but in +02:00. And "Date()" assumes the current timezone, if no timezone is delivered.

现在乐趣开始了,因为我的电脑不在 UTC 时区,而是在 +02:00。如果未提供时区,则“Date()”假定当前时区。

As a result, my Date-Object then represented the date/time "20190621T00:00:00+02:00". And that′s a bit different then "20190621T00:00:00+00:00" what may database wanted to give me.

结果,我的日期对象表示日期/时间“20190621T00:00:00+02:00”。这与数据库想要给我的“20190621T00:00:00+00:00”有点不同。

So i had database log entries that came directly from the future :-)

所以我有直接来自未来的数据库日志条目:-)

Perhaps

也许

let databaseDate = new Date(Date.UTC(sourceDate));
let localDate = new Date(Date.UTC());

could help you?

能帮到你吗?

Warm regards

温暖的问候