• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

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

I am trying to convert the below date to a javascript Date() object. When I get it back from the server, it is a Timestamp object,

Screenshot from Firebase Firestore console:

enter image description here

When I try the following on a list of objects returned from firestore:

  list.forEach(a => {
    var d = a.record.dateCreated;
    console.log(d, new Date(d), Date(d))
  })

I get this output: enter image description here

Clearly the Timestamps are all different, and are not all the same date of Sept 09, 2018 (which happens to be today). I'm also not sure why new Date(Timestamp) results in an invalid date. I'm a bit of a JS newbie, am I doing something wrong with the dates or timestamps?

over 3 years ago · Santiago Trujillo
3 answers
Answer question

0

The constructor for a JavaScript Date doesn't know anything about Firestore Timestamp objects - it doesn't know what to do with them.

If you want to convert a Timestamp to a Date, use the toDate() method on the Timestamp.

over 3 years ago · Santiago Trujillo Report

0

You can use toDate() function along with toDateString() to display the date part alone.

const date = dateCreated.toDate().toDateString()
//Example: Friday Nov 27 2017

Suppose you want only the time part then use the toLocaleTimeString()

const time = dateCreated.toDate().toLocaleTimeString('en-US')
//Example: 01:10:18 AM, the locale part 'en-US' is optional
over 3 years ago · Santiago Trujillo Report

0

You can use Timestamp.fromDate and .toDate for converting back and forth.

// Date to Timestamp
const t = firebase.firestore.Timestamp.fromDate(new Date());

// Timestamp to Date
const d = t.toDate();
over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error