I am trying to get readable date from firestore

 ̄綄美尐妖づ 提交于 2020-04-18 06:26:08

问题


I am trying to get readable date from Timestamp data type in my firestore database.

for (var ticketDoc of ticketsSnapshot.docs) {
            var timeStamp = await ticketDoc.data().TimePreferred;
            console.log(timeStamp.toDate());
            var time = new Date(timeStamp).toDate();
            ticketDoc.data().TimePreferred = time;
            tickets.push(ticketDoc.data());
        }

I read the question about a similar problem at :

How do I convert a Firestore date/Timestamp to a JS Date()?

so, i tried to do same and i expect the output of readable date, although it gives me the correct result in

console.log(timeStamp.toDate());

but also it gives me an error. Console output as follow :-

2019-04-10T06:30:00.000Z
TypeError: (intermediate value).toDate is not a function

Not : I am trying to get readable date in postman


回答1:


Change the following line:

var time = new Date(timeStamp).toDate();

into this:

var time = new Date(timeStamp).toDateString();

From the docs:

A string representing the date portion of the given Date object in human readable form in American English.




回答2:


Have you tried changing this to

var time = (new Date(timeStamp)).toDateString();



回答3:


If the TimePreferred field in your document is a Timestamp, you can get a valid Date object from it by simply calling toDate() on it.

So:

for (var ticketDoc of ticketsSnapshot.docs) {
    var date = ticketDoc.data().TimePreferred.toDate();
}

None of these calls are asynchronous or returning a promise, so you don't need await.




回答4:


I don't know why this timestamp object doesn't have the .toDate() extension, but if it has the 'seconds' and 'nanoseconds' properties, you can turn it to a JS Data with

Date(data.seconds)


来源:https://stackoverflow.com/questions/55608589/i-am-trying-to-get-readable-date-from-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!