I have a Google Cloud function that parses the day of the month from a UTC date For example myDate = Wed Nov 13 2019 02:05:00 GMT+0000 (UTC) This date is actually Tues Nov 12 in San Francisco time (PDT or PST).
But when in my Google cloud function, when I try to get the day of the month from this date with myDate.getDate(), I get 13 instead of 12.
I believe the reason why it returns 13 is because the time zone of the machine that the cloud function is running on is based on UTC time. When I run this function on my local machine, I get 12 as the correct date because my local machine is on PST time.
How do I make Google Cloud Function operated in the PDT or PST time zones so that the getDate() function gives me the correct date in the PDT or PST time zone?
As of Node 14, which is now in beta for Firebase as of Dec 11, 2020, you should be able to just do
process.env.TZ = 'America/Los_Angeles'
As Doug said, it's not possible to configure the timezone on a Cloud Functions server instance.
However, you don't need a library. Google Cloud Functions using NodeJS 8 or 10 can get the date in a particular time zone using the JavaScript Internationalization API:
let myDate = new Date("2019-11-13T02:05:00Z"); // the value you gave in the question
let s = myDate.toLocaleDateString('en-US', { timeZone: 'America/Los_Angeles' })
//=> "11/12/2019"
If you just want the numeric day of the month, you can extract it from the string from there.
+(s.split('/')[1]) //=> 12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With