Javascript 云函数未抵消firestore查询的日期

Javascript 云函数未抵消firestore查询的日期,javascript,date,google-cloud-firestore,google-cloud-functions,Javascript,Date,Google Cloud Firestore,Google Cloud Functions,我有一个cron作业,每天下午12点运行。它应该抓取当天早上6点到下午12点之间添加的集合中的所有文档。我正在使用一个名为dateOrderAdded的字段来运行查询。这个字段位于时区GMT-4,所以当我运行查询时,我必须考虑到这一点 "use strict"; const functions = require("firebase-functions"); const admin = require("firebase-admin")

我有一个cron作业,每天下午12点运行。它应该抓取当天早上6点到下午12点之间添加的集合中的所有文档。我正在使用一个名为
dateOrderAdded
的字段来运行查询。这个字段位于时区GMT-4,所以当我运行查询时,我必须考虑到这一点

"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

exports.sendTwelvePmOrderSummary = functions.pubsub.schedule("0 12 * * *")
.timeZone("America/Caracas")
.onRun(async context => {
    //Get current date
    const today = new Date();

    //Set time to 6AM
    const todaySixAm = new Date(today.setHours(6,0,0,0));   

    //Set time to 12PM
    const todayTwelvePm = new Date(today.setHours(12,0,0,0)); 

    console.log(todaySixAm.valueOf());  //browser - 1609149600000 | cloud fn - 1609135200000
    console.log(todayTwelvePm.valueOf()); // browser - 1609171200000 | cloud fn - 1609156800000
    //Get all orders in-between today at 6AM and today at 12PM
    const orderCol = await admin.firestore().collection('orders').where('dateOrderAdded','>=',todaySixAm).where('dateOrderAdded','<=',todayTwelvePm).get();
   
    const orderDocs = orderCol.docs.map(doc=>doc.data())
    if(orderCol.size === 0 ){       
        //No orders placed - TODO: Send no orders placed email
        console.log('No orders placed');
    }else{
        //orders! - TODO: Send order placement summary
        console.log('Orders placed');
    } 
    return null;
}
“严格使用”;
常量函数=需要(“firebase函数”);
const admin=require(“firebase管理员”);
admin.initializeApp();
exports.sendtwelvepordersummary=functions.pubsub.schedule(“0 12***”)
.时区(“美洲/加拉加斯”)
.onRun(异步上下文=>{
//获取当前日期
const today=新日期();
//把时间定在早上6点
const todaySixAm=新日期(今天,设定时间(6,0,0,0));
//设定时间为下午12点
const todaytevelpm=新日期(今天,设定时间(12,0,0,0));
console.log(todaySixAm.valueOf());//browser-1609149600000 | cloud fn-1609135200000
console.log(todaytevelpm.valueOf());//browser-1609171200000 | cloud fn-1609156800000
//在今天上午6点到下午12点之间获得所有订单

const orderCol=await admin.firestore().collection('orders')。where('dateOrderAdded','>=',todaySixAm)。where('dateOrderAdded','),我建议您自己创建unix时间戳,而不是依赖云函数的
日期

下一行将有助于根据UTC-4时区生成适当的时间:

编辑:对于Nodejs 10运行时,格式不同,因此我必须调整日期以避免生成错误的日期,如果格式在您选择的运行时不同,只需调整日期以使其有效


这样,您就不必依赖托管代码的服务器的本地时间。

根据@RobG在评论中的建议,我将其用于setUTC小时

"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");


exports.sendTwelvePmOrderSummary = functions.pubsub
.schedule("0 12 * * *")
.timeZone("America/Caracas")
.onRun(async context => {

    //Timezone of stored data is GMT -4 
    //For query to be run correctly use UTC hours
    //6AM = 6+ 4 = 10 UTC hours

    const todaySixAm = new Date(new Date().setUTCHours(10,0,0,0));  

    //Set time to 11:59:59:999AM - 12 + 4 = 16
    //Set it to just before 12PM so results from other CRONs won't be duplicated
    const todayTwelvePm = new Date(new Date().setUTCHours(15, 59, 59,999));  


    //Get all orders in-between today at 6AM and today at 12PM
    const orderCol = await admin.firestore().collection('orders').where('dateOrderAdded','>=',todaySixAm).where('dateOrderAdded','<=',todayTwelvePm).get();

    const orderDocs = orderCol.docs.map(doc => doc.data());
    if (orderCol.size === 0)
        return { message: "No orders placed - TWELVE PM Summary" };
  
    console.log('Orders placed!');
    return null;
  }
“严格使用”;
常量函数=需要(“firebase函数”);
const admin=require(“firebase管理员”);
exports.sendtwelvepordersummary=functions.pubsub
.附表(“0 12***”)
.时区(“美洲/加拉加斯”)
.onRun(异步上下文=>{
//存储数据的时区为GMT-4
//要正确运行查询,请使用UTC小时数
//上午6点=6+4=10 UTC小时
const todaySixAm=新日期(new Date().setUTCHours(10,0,0));
//将时间设置为11:59:59:999AM-12+4=16
//将其设置为12点之前,以便不会复制来自其他cron的结果
const todaytevelpm=新日期(new Date().setUTCHours(15,5959999));
//在今天上午6点到下午12点之间获得所有订单
const orderCol=wait admin.firestore().collection('orders')。where('dateOrderAdded','>=',todaySixAm)。where('dateOrderAdded','当使用setHours时,您正在设置本地小时数。为了与云保持一致,您似乎需要使用setUTCHours设置UTC小时数。
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");


exports.sendTwelvePmOrderSummary = functions.pubsub
.schedule("0 12 * * *")
.timeZone("America/Caracas")
.onRun(async context => {

    //Timezone of stored data is GMT -4 
    //For query to be run correctly use UTC hours
    //6AM = 6+ 4 = 10 UTC hours

    const todaySixAm = new Date(new Date().setUTCHours(10,0,0,0));  

    //Set time to 11:59:59:999AM - 12 + 4 = 16
    //Set it to just before 12PM so results from other CRONs won't be duplicated
    const todayTwelvePm = new Date(new Date().setUTCHours(15, 59, 59,999));  


    //Get all orders in-between today at 6AM and today at 12PM
    const orderCol = await admin.firestore().collection('orders').where('dateOrderAdded','>=',todaySixAm).where('dateOrderAdded','<=',todayTwelvePm).get();

    const orderDocs = orderCol.docs.map(doc => doc.data());
    if (orderCol.size === 0)
        return { message: "No orders placed - TWELVE PM Summary" };
  
    console.log('Orders placed!');
    return null;
  }