我已將此時間戳保存在 Firestore 檔案中:
last_check_mmr: 14 dicembre 2021 15:39:01 UTC 1
如何使用Javascript檢查從該日期開始是否過去了一天?
uj5u.com熱心網友回復:
由于您使用 Cloud Functions 函式,因此使用Dayjs“決議、驗證、操作和顯示日期的極簡主義 JavaScript 庫”非常容易。
使用該diff()方法的類似以下內容應該可以解決問題:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const dayjs = require('dayjs');
// Let's imagine you use a scheduled Cloud Funciton
exports.scheduledFunction = functions.pubsub.schedule('...').onRun(async (context) => {
// Get the value of the timestamp, e.g. by fetching a Firestore document
const docRef = ...;
const snap = await docRef.get();
const last_check_mmr = snap.get('last_check_mmr');
const date = dayjs(last_check_mmr.toDate());
const now = dayjs();
console.log(date.diff(now, 'd'));
// If you get a value of 0, it means it is less than a day, if you get -1 or less it is more than a day
if (date.diff(now, 'd') < 0) {
// more than a day
}
return null;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/384985.html
標籤:javascript 火力基地 日期 谷歌云firestore 谷歌云功能
