简体   繁体   English

AWS Lambda 时区

[英]AWS Lambda Timezone

I've been searching for a while and can't find a definitive answer on that or any oficial documentation from AWS.我已经搜索了一段时间,但找不到关于该问题或来自 AWS 的任何官方文档的明确答案。

I have a Lambda function on which I need to get the current date according to my timezone, but I'm unsure on which premisses I may rely to do that, and as this is a critical app, I can't simply rely on a single test or empirical result.我有一个 Lambda function,我需要根据我的时区获取当前日期,但我不确定我可以依赖哪些前提,因为这是一个关键的应用程序,我不能简单地依赖单一测试或经验结果。

I would expect that lambda functions follow the timezone of the AWS region that they are hosted in OR a unique timezone for any GMT 0, but couldn't confirm it anywhere.我希望 lambda 函数遵循它们所在的 AWS 区域的时区或任何 GMT 0 的唯一时区,但无法在任何地方确认。

Could anyone please clarify how this works and if possible also point to any official documentation about that?任何人都可以澄清这是如何工作的,如果可能的话还可以指出任何官方文件吗? Also, any special concerns related to daylight saving time?另外,与夏令时有关的任何特殊问题?

My Lambda function is written on NodeJS and what I've been doing locally (on my machine) to retrieve the current date time is using new Date() .我的 Lambda function 是用 NodeJS 编写的,我在本地(在我的机器上)使用new Date()检索当前日期时间。

Lambda time using the date object will return a date in UTC unix time (as described here ). LAMBDA时间使用日期对象将返回UTC UNIX时间的日期(如描述在这里 )。

To get a time in a specific timezone you can use moment-timezone . 要在特定时区获得时间,您可以使用时刻时区

Using Javascript Date().now(): 使用Javascript Date()。now():

var moment = require("moment-timezone");
moment(Date().now()).tz("America/Los_Angeles").format("DD-MM-YYYY");

or using new Date(): 或使用新的Date():

var moment = require("moment-timezone");
var date = new Date();
moment(date.getTime()).tz("America/Los_Angeles").format("DD-MM-YYYY");

You can change the format to whatever you require, see more formats here 您可以将格式更改为您需要的格式,请在此处查看更多格式

Regarding daylight savings time, it is possible to detect it using isDST() method as shown here 关于日光节约时间,有可能使用isDST()方法来检测它,如图这里

To avoid using an external library, you can use toLocaleString() which relies on Intl.DateTimeFormat() .为避免使用外部库,您可以使用依赖于Intl.DateTimeFormat()toLocaleString( ) 。 You can pass it a locale and an option object that supports a " timezone " and other formating parameters.您可以向它传递一个语言环境和一个支持“时区”和其他格式参数的选项 object。

 var today = new Date(); //UTC in lambda var localeDate = today.toLocaleString('fr-FR', { timeZone: 'Europe/Paris'}); // to get the full date string var localeHour = today.toLocaleString('fr-FR', { timeZone: 'Europe/Paris',hour: 'numeric' }); //to get just the hour console.log(localeDate); console.log(localeHour);

If @bbas answer didn't work for you in nodejs14.x, try this out as mentioned in :如果 @bbas 的回答在 nodejs14.x 中对你不起作用,请尝试以下内容中提到

 let date = new Date(); // UTC in lambda. let dateString = new Intl.DateTimeFormat('en-US', { timeZone: 'Asia/Kolkata', year: 'numeric', month: '2-digit', day: '2-digit'}).format(date); console.log(dateString); // prints out date in mm/dd/yyyy in 'Asia/Kolkata' timezone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM