Home > Software engineering >  timestamp to IST reactjs
timestamp to IST reactjs

Time:05-27

How to compare the firebase timestamp with Indian Standard Time (IST). We have to compare everything like date and time. If both are equal we should give a message in alert box as Success otherwise alert as not-matchedThis is the image of firebase timestamp

This is the image for IST date

CodePudding user response:

Fetch the date from firebase, and call Date.parse() on it to convert it to an epoch date. Then do the same on your IST date. If you want the current time in an epoch date use Date.now().

Something like this:

const updatedDateEpoch = Date.parse(*YOUR DATE FROM FIREBASE*)

// if you want the current time use this date
const currentEpoch = Date.now()

// IST date to epoch
const istEpoch = Date.parse(*THE IST DATE YOU HAVE*)

// switch currentEpoch to istEpoch if you don't want to use the current time.
if (currentEpoch === updatedDateEpoch){
    alert("Success!")
}else{
    alert("Not matched.")
}

CodePudding user response:

You can convert the firestore timestamp to javascript timestamp using .toDate() and then convert the js timestamp to the desired timezone - you can use sophisticated moment-timezone library method moment().tz() or the js date library method toLocaleString().

fbTs.toDate().toLocaleString(undefined, { timeZone: 'Asia/Kolkata' }); 

fbTs is the firestore timestamp

For comparison, it is better to convert both the timestamps - the firebase timestamp and the current timestamp in epoch timestamp and compare. getTime() method will get you this. `

if (fbTs.toDate().getTime() === new Date().getTime()) { 
  ...
} else { 
  ...
}

  • Related