在TypeScript中使用AM/PM的日期之间筛选对象数组

在TypeScript中使用AM/PM的日期之间筛选对象数组,typescript,google-cloud-firestore,Typescript,Google Cloud Firestore,我正在Angular 8(TypeScript)和Firebase Firestore中做一个web应用程序。我有一个名为createdAt的文档,其类型为firebase.firestore.Timestamp。我正在使用AM/PM保存Firestore中的日期 export interface Client { id: number; name: string; createdAt: firebase.firestore.Timestamp; } 我要筛选的数组是(这是我从Fi

我正在Angular 8(TypeScript)和Firebase Firestore中做一个web应用程序。我有一个名为
createdAt
的文档,其类型为
firebase.firestore.Timestamp
。我正在使用AM/PM保存Firestore中的日期

export interface Client {
  id: number;
  name: string;
  createdAt: firebase.firestore.Timestamp;
}
我要筛选的数组是(这是我从Firestore收到的):

我尝试按如下方式过滤阵列:

  filterClients() {
    const from = new Date(this.fromDate.year, this.fromDate.month - 1, this.fromDate.day, 0, 0, 0);
    const to = new Date(this.toDate.year, this.toDate.month - 1, this.toDate.day, 23, 59, 0);

      this.clients = this.clientsCopy.filter(client => 
        client.createdAt.toDate().getTime() >= from.getTime() && 
        client.createdAt.toDate().getTime() <= to.getTime());
  }
filterClients(){
const from=新日期(this.fromDate.year,this.fromDate.month-1,this.fromDate.day,0,0);
const to=新日期(this.toDate.year,this.toDate.month-1,this.toDate.day,23,59,0);
this.clients=this.clientsCopy.filter(客户端=>
client.createdAt.toDate().getTime()>=from.getTime()&&

client.createdAt.toDate().getTime()您的问题可能与在JavaScript中正确使用本地时区的
Date
API有关。如果您通过Unix时间戳比较两个日期(
Date.getTime()
),则必须确保两者都基于UTC或本地时区(或其他统一时区)在日期的施工时间

我可以想象,时间戳以UTC格式保存在Firebase中。当您这样定义日期筛选器范围时

const from = new Date(2019, 7, 2, 0, 0, 0);
const to = new Date(2019, 7, 3, 23, 59, 0);
将构造一个考虑本地时区并调整其基础Unix时间戳的日期(请参阅)。
Firebase:UTC客户端:本地时区,因此您可以将苹果与桔子进行比较

您可以使用来构建客户日期:

const from = new Date(Date.UTC(2019, 7, 2, 0, 0, 0));
const to = new Date(Date.UTC(2019, 7, 3, 23, 59, 0));
或者使用其中一个日期

希望,这会有所帮助

const from = new Date(Date.UTC(2019, 7, 2, 0, 0, 0));
const to = new Date(Date.UTC(2019, 7, 3, 23, 59, 0));