0

I am trying to figure out the best approach to a problem. I have a firebase cloud function that listens to changes made on a firebase database path. Once a change in the database path is made, the cloud function gets triggered and sends a notification to a user.

Within this cloud function, I am storing details of the notification in the database (another path) so the user can come back and view their past notifications within my app.

The problem I am facing is storing the correct date and time. Currently, I am using the new Date() function to capture this information within the cloud function but this is obviously wrong as it is returning the server time. I need to capture the client time (the person who is receiving the notification) and store that value in the database.

The thing is, the client can be in any country so I need to account for different time zones. How do I do this? I feel like there must be a simple approach to this but I'm just not seeing it.

===

For anyone who comes across this post looking for a solution, I found the solution on this post:

Convert UTC date time to local date time

It works just the fine on Firebase Cloud Function code :-)

AppFirl
  • 165
  • 2
  • 15

1 Answers1

0

You can save date from server with timezone offset or date with GMT timezone. As you have timezone offset, you can easily convert it to any timezone.

For ex.

var d = new Date('2000-01-01');
// You can save date in GMT timezone like this.
// It will be like 1999-12-31T18:30:00.000Z.
// Currently I am with +5:30 timezone.
console.log(d.toJSON());

// Convert date string back to date object at client side.
var d2 = new Date('1999-12-31T18:30:00.000Z');
console.log(d2.toString());
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
  • Thanks for helping but it doesn't seem to be working as I expected. Let me paint the scenario - on the server side, I save the date. On the client side, if I convert the date in the US, it should show me 3:22PM (for example). However, if I change the timezone on my computer to Norway and I convert the date, it should now show me 9:23PM. How do I achieve this? – AppFirl Mar 08 '18 at 20:23