I have this following node js code:
let pureDate = new Date('2022-12-22T00:00:00.000Z')
console.log(pureDate)
let formatted1 = moment(pureDate).format('YYYY-MM-DDTHH:00:00+hh:mm')
console.log(formatted1)
let formatted2 = moment.utc(pureDate).local().format('YYYY-MM-DDTHH:00:00+hh:mm')
console.log(formatted2)
When I run it at my Windows 10 local machine (+07:00), it print to console:
2022-12-22T00:00:00.000Z
2022-12-22T07:00:00+07:00
2022-12-22T07:00:00+07:00
Now I change timezone to +12:00 and rerun the code, it print:
2022-12-22T00:00:00.000Z
2022-12-22T12:00:00+12:00
2022-12-22T12:00:00+12:00
Everything were fine
Now I deploy it to aws lambda using cloudformation:
Resources:
timerHandler:
Type: AWS::Serverless::Function
Properties:
Handler: src.timerHandler
Runtime: nodejs16.x
Environment:
Variables:
NODE_ENV: lc
Timeout: 100
Events:
ScheduledEvent:
Type: Schedule
Properties:
Schedule: "rate(15 minutes)"
After sometime, I access the log and see (please scroll to right-end):
2022-12-22T22:47:04.451+07:00 2022-12-22T15:47:04.451Z e55ee401-87fd-4614-a7c6-971973ccccc6 INFO pureDate: 2022-12-22T00:00:00.000Z
2022-12-22T22:47:04.451+07:00 2022-12-22T15:47:04.451Z e55ee401-87fd-4614-a7c6-971973ccccc6 INFO formatted1: 2022-12-22T00:00:00+12:00
2022-12-22T22:47:04.451+07:00 2022-12-22T15:47:04.451Z e55ee401-87fd-4614-a7c6-971973ccccc6 INFO formatted2: 2022-12-22T00:00:00+12:00
The time 00:00:00 is correct, but the zone +12:00 is not. I would expect the formatted should be 2022-12-22T12:00:00+12:00 or 2022-12-22T00:00:00+00:00
This is zone information of the lambda execution:
2022-12-22T23:46:32.052+07:00 2022-12-22T16:46:32.052Z f1d04019-44e7-463a-af1d-faf2a5043887 INFO zone_name: Africa/Abidjan
2022-12-22T23:46:32.052+07:00 2022-12-22T16:46:32.052Z f1d04019-44e7-463a-af1d-faf2a5043887 INFO timezone: GMT
Is that the problem of momentjs, or my code, or lambda? How can I fix it?
moment version: “^2.29.4”
I see the final logs was not printed by the first source code:
Could you provide the source code of the final logs?
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
for the zone name
The last log I totally forget 😀