Skip to content

Saving Date to firestore document in Javascript

I have a function that converts date string to Date

const convertStringToDate = (string) => {
  const fragments = string.split('/')
  return new Date(fragments[2], fragments[1] - 1, fragments[0])

Then I use as following:

const birthdate = convertStringToDate('19/11/1986')

await reference.set({ birthdate  })

But on firebase console, the birthdate is stored as an empty array as the image bellow:

What I am doing wrong?


convertStringToDate returns a Date object, which if you console log, will show up something like this:

[object Date] { ... }

If you want to store it as a string, you need to convert that Date object to a string with something like birthdate.toIsoString(). But because this is Firebase, if you want to store an actual date, you want to convert it to a Firestore timestamp:

// On the client
await reference.set({ birthdate: firebase.firestore.Timestamp.fromDate( birthdate ) })

// Or on the server
await reference.set({ birthdate: admin.firestore.Timestamp.fromDate( birthdate ) })

When you retrieve it later, you’d use:

// To get a date object
const birthdate =

// To get the date in milliseconds
const birthdate =