30

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

GCSDC
  • 3,138
  • 3
  • 28
  • 48
  • 2
    JavaScript Date objects represent a specific point in time. Time isn't affected by timezones and neither are Date objects. `new Date` is always correct. It's not until you convert it to a format that does (or can) include timezone information (like a string) where it could become incorrect. – Paul Mar 27 '19 at 18:50
  • @Paulpro got your point. Thanks! – GCSDC Mar 27 '19 at 19:21
  • 4
    FWIW, the system timezone in the Lambda environment [is documented as UTC, and the clocks are kept in sync with NTP](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html). – Michael - sqlbot Mar 27 '19 at 21:31
  • For anyone concerned, as of December 2019 this information can be found under the "Environment Variables" section: "The environment's timezone (UTC). The execution environment uses NTP to synchronize the system clock." – Gábor Nagy Dec 17 '19 at 12:14

3 Answers3

30

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.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Deiv
  • 3,000
  • 2
  • 18
  • 30
  • 1
    Thanks for your answer! Regarding "Lambda time using the date object will return a date in UTC unix time." could you please point to any AWS documentation about that. Will this solution automatically update day, month, year and so on (if converting from um tz to other requires that)? Also, will daylight saving time be handled correctly? – GCSDC Mar 27 '19 at 19:23
  • Thanks for your edits! I was going to add to it but it slipped my mind – Deiv Mar 28 '19 at 18:24
  • 1
    No worries. Glad that with your answer and the comments from other users we could solve the issue and provide a good quality answer. Thanks again! – GCSDC Mar 28 '19 at 18:28
  • Is there a way to do it without a library? Honestly, this does not make any sense lambda in ICT zone should return ICT time. Btw your first link is not working – Matteo Apr 22 '20 at 00:20
  • I just checked, the link still works (it points to https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html). However, it does make sense to use UTC time because if you take for example an AWS region that could span multiple timezones, it shouldn't change based on where it spawns. That's why they use a common time format (UTC is also used for ms time in all software). That being said, you can change the timestamp without a lib if you know how many hours to offset (https://stackoverflow.com/a/1050782/7207514). A lib just let's you handle it better since it accounts for edge cases – Deiv Apr 23 '20 at 02:55
  • FYI, the value of `process.env.TZ` is `UTC` (rather than `Etc/UTC` and equivalents) – Piran Nov 03 '21 at 10:44
  • It seems what would suit OP question the best is setting TZ env variable to the time zone needed. Hard-coding it in the app code is prone to error (unless that's a very specific use case, i.e. converting timestamp between different timezones). – topr Sep 26 '22 at 11:16
3

To avoid using an external library, you can use toLocaleString() which relies on Intl.DateTimeFormat(). You can pass it a locale and an option object that supports a "timezone" and other formating parameters.

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);
Brad
  • 15,186
  • 11
  • 60
  • 74
bbas
  • 31
  • 1
0

If @bbas answer didn't work for you in nodejs14.x, try this out as mentioned in :

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
user11666461
  • 761
  • 7
  • 12