DEV Community

anastacioSalazar
anastacioSalazar

Posted on • Updated on

How to convert Firestore ServerTimestamp to Date in firebase v9?

Image description

import { getFirestore,serverTimestamp} from "firebase/firestore";
//
const newTimeStamp=Timestamp.now();
Enter fullscreen mode Exit fullscreen mode

For the format to be understandable for the user we have to do a conversion.

    const ConvertDate= (d) => {
        const newDate = new Date(d.seconds*1000)
        return newDate.toLocaleString();
      }
Enter fullscreen mode Exit fullscreen mode

When we want to get the format we just call this function done with javascript

const normalformat=ConvertDate(newTimeStamp);
Enter fullscreen mode Exit fullscreen mode

Result
print 17/8/2022, 10:45:33

or

const newTimeStamp=Timestamp.now().toMillis();
const newDat=ConvertDate2(newTimeStamp);
    const ConvertDate2= (d) => {
        const newDate = new Date(d)
        return newDate.toLocaleString();
      }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)