Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用当前日期作为条件从firebase检索数据_Javascript_Angular_Google Cloud Firestore_Angularfire2 - Fatal编程技术网

Javascript 使用当前日期作为条件从firebase检索数据

Javascript 使用当前日期作为条件从firebase检索数据,javascript,angular,google-cloud-firestore,angularfire2,Javascript,Angular,Google Cloud Firestore,Angularfire2,我需要通过将当前日期与firestore数据库中名为start date的属性进行比较来检索数据。 我只需要检索与当前日期值和开始日期匹配的数据。 我创建了一个查询来检索数据,但无法使用该查询检索任何数据 event.model.ts export interface Events{ title:String; start:Date; end:Date; } app.component.ts event$:Observable<Events[]>;

我需要通过将当前日期与firestore数据库中名为start date的属性进行比较来检索数据。 我只需要检索与当前日期值和开始日期匹配的数据。 我创建了一个查询来检索数据,但无法使用该查询检索任何数据

event.model.ts

    export interface Events{
    title:String;
    start:Date;
    end:Date;

}
app.component.ts

event$:Observable<Events[]>;
     const newDate = firebase.firestore.Timestamp.fromDate(new Date()).toDate();

  this.event$ = this.firestore.collection<Events>('eventsCalender',
  ref => ref.where('start','==',newDate)).valueChanges();



   this.event$.subscribe(data => console.log(data));
事件$:可观察;
const newDate=firebase.firestore.Timestamp.fromDate(新日期()).toDate();
this.event$=this.firestore.collection('eventsCalender',
ref=>ref.where('start','=',newDate)).valueChanges();
this.event$.subscribe(数据=>console.log(数据));
我已经创建了一个接口并导出,在app.component.ts中实现了检索事件的查询。start是开始日期变量,我在其中检查开始变量是否等于当前日期变量,如果是,则返回这些事件,否则不存在当前日期事件。
但我编写的代码即使在数据库中有数据时也不会获取任何数据。

在Firestore中存储日期时,它的存储粒度为微秒。在JavaScript中创建
新日期()
时,其粒度为毫秒。在不同时刻创建的两个这样的日期完全相等的可能性非常小

您通常需要执行以下操作之一:

  • 将所有日期四舍五入存储到一定的粒度。例如:如果您只想存储日期,而不想存储时间,则可以确保将所有日期字段存储为该日期的午夜。然后在查询中可以执行:
    var newDate=newDate();newDate.setHours(0,0,0,0)比较午夜和午夜
  • 查询范围。例如:要获取开始日期为今天的所有字段,可以执行以下操作:

    var startOfToday = new Date(); 
    startOfToday.setHours(0,0,0,0);
    var endOfToday = new Date(); 
    endOfToday.setHours(23,59,59,999);
    ref.where('start','>=',startOfToday).where('start', '<=', endOfToday)
    
    var startOfToday=新日期();
    今天开始。设定时间(0,0,0,0);
    var endOfToday=新日期();
    截至今日设定时间(23,59,59999);;
    
    参考where('start','>=',StartToToToToToDay)。where('start','谢谢,我会试试看