angular - 如何在 Angular 中将数据保存为 Firestore 时间戳对象
问题描述
只想将日期保存为 Firestore 数据库中的时间戳,但它将时间戳存储为数字:
save() {
const data = this.transactionForm.value;
data.type = data.type == 'true' || data.type == true ? true : false;
data.updated = firebase.firestore.FieldValue.serverTimestamp();
// Date string to timestamp
data.date = new Date(data.date.split("-").reverse().join("-")).getTime();
let id = data.id;
this.transCollection.doc(id).update(data);
}
解决方案
unixToTimestamp() {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
console.log(hours + ':' + minutes + ':' + seconds);
console.log(day + '/' + month + '/' + year);
}
我在打字稿中尝试了这个,我得到了以下输出:
11:25:07
19/11/2018
通过这种方式,您可以选择创建时间戳并将其保存在 Firestore 中的方式
您也可以按照您想要的方式打印月份。
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const month = months[date.getMonth()];
这是因为 date.getMonth() 获取索引,这就是我使用 +1 获取一个月的原因。
推荐阅读
- python-3.x - Scikitlearn Column Transformer Error: Column ordering must be equal for fit and for transform when using the remainder keyword
- python - 在 AWS Lambda 中使用 Python SQLAlchemy 是个好主意吗?
- ruby-on-rails - 将 Rails 中 select_tag 中的参数传递给一个 script.rb 获取此参数
- css - 为什么 a:hover 不适用于引导程序源?
- android - 有没有办法在 BottomSheetDialogFragment 中使用 AlertDialog?
- c - 为什么我不能将我的“for”索引直接传递给 pthread_create 调用?
- javascript - 在反应引导程序中设置 Form.Check(复选框)控件的样式
- c# - 通过 Selenium 在文件中写入记录的浏览器操作
- django - 如何使用 django&postgre 在家里安装服务器
- java - 如何避免调用 handler.postDelayed(Runnable run)?