Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda Timezone

Tags:

I've been searching for a while and can't find a definitive answer on that or any oficial documentation from 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.

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.

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().

like image 451
GCSDC Avatar asked Mar 27 '19 18:03

GCSDC


People also ask

What timezone does AWS Lambda use?

FWIW, the system timezone in the Lambda environment is documented as UTC, and the clocks are kept in sync with NTP. For anyone concerned, as of December 2019 this information can be found under the "Environment Variables" section: "The environment's timezone (UTC).

What is runtime in AWS Lambda?

Lambda re-uses the execution environment from a previous invocation if one is available, or it can create a new execution environment. A runtime can support a single version of a language, multiple versions of a language, or multiple languages.

Is AWS Lambda function region specific?

When working with AWS Lambda functions, the question of region is one of the first you need to answer. As each Lambda function lives in a specific AWS region, and each AWS region has a slightly different set of functionality, you may find yourself having to work with functions in multiple regions on a regular basis.

How do I change the runtime in Lambda?

To update the runtime, just go into the Lambda console -> Code and Scroll to Runtime Settings to change the runtime. Depending what code your Lambda has jumping from Python 2 to 3 will probably not run - so just changing the runtime might not be the onlything you need to do.


1 Answers

Lambda time using the date object will return a date in UTC unix time

TZ – The environment's time zone (UTC).

To get a time in a specific timezone you can use moment-timezone.

Using Javascript Date().now():

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

or using new 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 moment .js format to whatever you require

Regarding daylight savings time, it is possible to detect it using isDST() method.

like image 177
Deiv Avatar answered Sep 19 '22 14:09

Deiv