mayank3503
mayank3503

Reputation: 367

convert firestore timestamp to date into different format

the timestamp variable is stored in the firestore , and when extracted it is then in the format of seconds and nanoseconds , how to convert the timestamp to the format like 2018-09-19T00:00:00

let Ref=firebase.firestore().collection("Recruiter").doc(u.uid).collection("Jobs")
    Ref.orderBy("timestamp", "desc").onSnapshot(function(snapshot){
      $.each(snapshot.docChanges(), function(){
        var change= this
        if(change.type==="added"){
           var ab= new Date(change.doc.data().timestamp) 
           console.log(ab)
          thisIns.RecruiterChart.chartOptions.xaxis.categories.push(
            ab
            ) 

           console.log( thisIns.RecruiterChart.chartOptions.xaxis.categories)
        }

      })
    })

the variable ab is showing "invalid date" on the console

Upvotes: 5

Views: 8928

Answers (3)

user16806454
user16806454

Reputation: 421

var ab = change.doc.data().timestamp.toDate().toLocaleDateString('en-US') // 7/2/2021

or more:

var ab = change.doc.data().timestamp.toDate().toLocaleDateString('en-US', { weekday:"long", year:"numeric", month:"short", day:"numeric"}) // Friday, Jul 2, 2021

Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Upvotes: 0

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

You should use the toDate() method of the Firestore Timestamp:

Convert a Timestamp to a JavaScript Date object. This conversion causes a loss of precision since Date objects only support millisecond precision.

Returns Date

JavaScript Date object representing the same point in time as this Timestamp, with millisecond precision.

So, you would do the following:

var timestampDate = change.doc.data().timestamp.toDate(); 
console.log(timestampDate);

Then, you need to format this date as desired. The easiest is to use a dedicated library like moment.js, as follows:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

// ...

<script>

   // ...

    var timestampDate = change.doc.data().timestamp.toDate(); 
    var m = moment(timestampDate );
    var mFormatted = m.format();    // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds) 
    console.log(mFormatted );

    // ...

</script>

Other possibilities for formatting a date with moment.js are to be found here.

Upvotes: 8

Diogo Lessa
Diogo Lessa

Reputation: 177

I think I know whats happening.

Your ab variable should be in string format, right?

Try parsing it to Number first.. and see what happens.

Try logging something like:

var ab= new Date(Number(change.doc.data().timestamp))
console.log(ab)
thisIns.RecruiterChart.chartOptions.xaxis.categories.push(
  ab
) 

console.log( thisIns.RecruiterChart.chartOptions.xaxis.categories)

OR

var ab= new Date(parseInt(change.doc.data().timestamp, 10))
console.log(ab)
thisIns.RecruiterChart.chartOptions.xaxis.categories.push(
  ab
) 
console.log( thisIns.RecruiterChart.chartOptions.xaxis.categories)

Cheers,

Upvotes: 1

Related Questions