admin 管理员组文章数量: 1086019
I am trying to get my local date time in Cloud Functions using new Date()
, but the result is with offset + 3.
Is there any way to get the correct offset? Without convert, using new Date().getTimezoneOffset
I am trying to get my local date time in Cloud Functions using new Date()
, but the result is with offset + 3.
Is there any way to get the correct offset? Without convert, using new Date().getTimezoneOffset
- Can you provide the output? – keser Commented Nov 22, 2019 at 14:44
- What do you mean by "local" date time? Your puter timezone? – Freez Commented Nov 22, 2019 at 14:56
- @aomerk 2019-11-25T11:18:56 , but the correct output should be 2019-11-25T08:18:56 – edilson14 Commented Nov 25, 2019 at 11:23
- @Freez my location , brazilian – edilson14 Commented Nov 25, 2019 at 11:24
-
@edilson14 get IP address from request and you can than use IP addresses location to get correct Date, if it is what you want. If you only want to output Brazilian look up
localeString
and how to use it – keser Commented Nov 25, 2019 at 11:28
3 Answers
Reset to default 7Cloud Functions run in GMT timezone, regardless of the environment's actual location. If you want the date/time in a specific timezone, you will have to convert it to that timezone yourself in your code.
A JavaScript Date object just represents a point in time for all people on the planet, without respect to timezone. If you print the Date object, you will see a timezone, but that's just because the code that renders the date is using the timezone of the clock configured on the puter. It's still the same point in time not matter what puter the date, or what timezone it's configured for.
If you want to format the date for people in a specific timezone, you will have to write some code for that. A mon library to help with that is momentjs, using its timezone plugin.
Here is the method I created for Cloud Functions, hope it helps:
function datetimeLocal(timezone : number) : Date {
const localdate = new Date(Date.parse(new Date().toLocaleString('en-US', { timeZone: 'UTC' })) + (timezone * 60 * 60 * 1000));
return localdate;
}
Then you can just call this function, passing the desired local timezone, eg.: datetimeLocal(-3) and you'll receive a new Date() object according to the timezone you need.
I repeat, for it to work, DONT NEED TO CHANGE THE STRINGS 'en-US' neither 'UTC'.
本文标签: javascriptGet new Date in Cloud Functions in specific time zoneStack Overflow
版权声明:本文标题:javascript - Get new Date in Cloud Functions in specific time zone - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744008391a2517698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论