blu*_*her 25 javascript firebase google-cloud-firestore
我想将以下日期转换为javascript Date()对象.当我从服务器返回时,它是一个Timestamp对象,
Firebase Firestore控制台的屏幕截图:
当我在firestore返回的对象列表上尝试以下操作时:
list.forEach(a => {
var d = a.record.dateCreated;
console.log(d, new Date(d), Date(d))
})
Run Code Online (Sandbox Code Playgroud)
显然,时间戳都是不同的,并不是2018年9月9日(恰好是今天)的相同日期.我也不确定为什么会new Date(Timestamp)导致invalid date.我是一个JS新手,我对日期或时间戳做错了吗?
Dou*_*son 57
JavaScript Date的构造函数对Firestore Timestamp对象一无所知- 它不知道如何处理它们.
如果要将时间戳转换为日期,请在时间戳上使用toDate()方法.
Wal*_*riq 31
除了其他答案之外,你也可以这样做
//date from firebase is represented as
let time = {
seconds: 1613748319,
nanoseconds: 47688698687,
}
const fireBaseTime = new Date(
time.seconds * 1000 + time.nanoseconds / 1000000,
);
const date = fireBaseTime.toDateString();
const atTime = fireBaseTime.toLocaleTimeString();
console.log(date, atTime);Run Code Online (Sandbox Code Playgroud)
小智 30
您可以使用 toDate() 函数和 toDateString() 来单独显示日期部分。
const date = dateCreated.toDate().toDateString()
//Example: Friday Nov 27 2017
Run Code Online (Sandbox Code Playgroud)
假设您只想要时间部分,然后使用 toLocaleTimeString()
const time = dateCreated.toDate().toLocaleTimeString('en-US')
//Example: 01:10:18 AM, the locale part 'en-US' is optional
Run Code Online (Sandbox Code Playgroud)
小智 22
请使用 toDate() 方法,然后使用像这样的角管将其转换为格式 -
{{ row.orderDate.toDate() | 日期:'dd MMM hh:mm' }}
Waj*_*ath 19
您可以使用Timestamp.fromDate和.toDate用于来回转换。
// Date to Timestamp
const t = firebase.firestore.Timestamp.fromDate(new Date());
// Timestamp to Date
const d = t.toDate();
Run Code Online (Sandbox Code Playgroud)
Dil*_*age 16
如何将 Unix 时间戳转换为 JavaScript 日期对象。
var myDate = a.record.dateCreated;
new Date(myDate._seconds * 1000); // access the '_seconds' attribute within the timestamp object
Run Code Online (Sandbox Code Playgroud)
Nag*_*aba 13
终于,我得到了我需要的东西。这将返回日期为08/04/2020
new Date(firebase.firestore.Timestamp.now().seconds*1000).toLocaleDateString()
Run Code Online (Sandbox Code Playgroud)
小智 9
一种简单的方法是使用toMillis()firestore 时间戳上的方法将 firestore 时间戳转换为纪元时间戳。
例如:您有一个 firestore 时间戳
created_on : Timestamp { _seconds: 1622885490, _nanoseconds: 374000000 }
let epochTimestamp = created_on.toMillis()
//epochTimestamp = 1621081015081
//Now this timestamp can be used as usual javascript timestamp which is easy to manipulate.
let date = new Date(epochTimestamp) //date = Sat May 15 2021 17:46:55 GMT+0530 (India Standard Time)
Run Code Online (Sandbox Code Playgroud)
小智 6
从 firestore 获取的时间戳对象有一个toDate()可以使用的方法。
list.forEach(a => {
var d = a.record.dateCreated;
console.log(d.toDate())
})
Run Code Online (Sandbox Code Playgroud)
toDate()这是 firebase 文档中有关该方法的引用
将时间戳转换为 JavaScript 日期对象。此转换会导致精度损失,因为 Date 对象仅支持毫秒精度。
返回表示与此 Timestamp 相同的时间点的 Date JavaScript Date 对象,精度为毫秒。
https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp#todate
这可能有帮助:
new Date(firebaseDate._seconds * 1000).toUTCString()
Run Code Online (Sandbox Code Playgroud)
小智 5
const timeStampDate = record.createdAt;
const dateInMillis = timeStampDate._seconds * 1000
var date = new Date(dateInMillis).toDateString() + ' at ' + new Date(dateInMillis).toLocaleTimeString()
Run Code Online (Sandbox Code Playgroud)
输出示例: Sat 11 Jul 2020 at 21:21:10
| 归档时间: |
|
| 查看次数: |
18350 次 |
| 最近记录: |