我正在使用javascript开发我不熟悉的firebase云函数。我需要为已经续订的用户添加n天数。
以下是该函数的一部分
...
var sellerRef = admin.firestore().doc('sellerProfile/'+req.query.sellerId)
var seller = sellerRef.get().then(snapshot=>{
const data= snapshot.data()
var currentDate = data.subscriptionDeadline.toDate()
Date.prototype.addDays= function(d){
this.setDate(this.getDate() + d);
return this;
};
var newDate = currentDate.addDays(30);
res.send(newDate)
})
sellerRef.update({
subscriptionDeadline: Date.parse(newDate)
})
...在这段代码中,我得到了一个人的订阅截止日期,然后使用一个原型添加了30天作为订阅续订。如何将日期转换回时间戳,以便能够将新的订阅截止日期保存到firestore,或者如果我可以添加30天而不将时间戳转换为日期,效果会更好。
发布于 2020-10-16 05:07:36
Firestore时间戳对象不提供任何日期数学运算。如果你把时间戳转换成日期(或者其他可以让你做数学运算的东西),再把时间戳转换回时间戳,那就更容易了。
const timestamp = data.subscriptionDeadline;
const date = timestamp.toDate();
const laterDate = new Date(date.getTime() + 30*60*60*1000);
const laterTimestamp = admin.firestore.Timestamp.fromDate(laterDate);https://stackoverflow.com/questions/64379607
复制相似问题