Avatar
0
anaconda875 Beginner
anaconda875 Beginner
momentjs aws-lambda wrong timezone
I have this following node js code:
<code><span>let</span> pureDate = <span>new</span> <span>Date</span>(<span>'2022-12-22T00:00:00.000Z'</span>)
<span>console</span>.<span>log</span>(pureDate)
<span>let</span> formatted1 = <span>moment</span>(pureDate).<span>format</span>(<span>'YYYY-MM-DDTHH:00:00+hh:mm'</span>)
<span>console</span>.<span>log</span>(formatted1)
<span>let</span> formatted2 = moment.<span>utc</span>(pureDate).<span>local</span>().<span>format</span>(<span>'YYYY-MM-DDTHH:00:00+hh:mm'</span>)
<span>console</span>.<span>log</span>(formatted2)
</code>

When I run it at my Windows 10 local machine (+07:00), it print to console:

<code><span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>.000Z
<span>2022</span>-<span>12</span>-22<span>T07</span>:<span>00</span>:<span>00</span>+<span>07</span>:<span>00</span>
<span>2022</span>-<span>12</span>-22<span>T07</span>:<span>00</span>:<span>00</span>+<span>07</span>:<span>00</span>
</code>

Now I change timezone to +12:00 and rerun the code, it print:

<code><span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>.000Z
<span>2022</span>-<span>12</span>-22<span>T12</span>:<span>00</span>:<span>00</span>+<span>12</span>:<span>00</span>
<span>2022</span>-<span>12</span>-22<span>T12</span>:<span>00</span>:<span>00</span>+<span>12</span>:<span>00</span>
</code>

Everything were fine

Now I deploy it to aws lambda using cloudformation:

<code><span>Resources</span>:
  <span>timerHandler</span>:
    <span>Type</span>: <span>AWS</span>::<span>Serverless</span>::<span>Function</span>
    <span>Properties</span>:
      <span>Handler</span>: src.<span>timerHandler</span>
      <span>Runtime</span>: nodejs16.<span>x</span>
      <span>Environment</span>:
        <span>Variables</span>:
          <span>NODE_ENV</span>: lc
      <span>Timeout</span>: <span>100</span>
      <span>Events</span>:
        <span>ScheduledEvent</span>:
          <span>Type</span>: <span>Schedule</span>
          <span>Properties</span>:
            <span>Schedule</span>: <span>"rate(15 minutes)"</span>
</code>

After sometime, I access the log and see (please scroll to right-end):

<code><span>2022</span>-<span>12</span>-22<span>T22</span>:<span>47</span>:<span>04</span><span>.451</span>+<span>07</span>:<span>00</span>   <span>2022</span>-<span>12</span>-22<span>T15</span>:<span>47</span>:<span>04</span>.451Z e55ee401-87fd-<span>4614</span>-a7c6-971973ccccc6 <span>INFO</span> <span>pureDate</span>: <span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>.000Z

<span>2022</span>-<span>12</span>-22<span>T22</span>:<span>47</span>:<span>04</span><span>.451</span>+<span>07</span>:<span>00</span>   <span>2022</span>-<span>12</span>-22<span>T15</span>:<span>47</span>:<span>04</span>.451Z e55ee401-87fd-<span>4614</span>-a7c6-971973ccccc6 <span>INFO</span> <span>formatted1</span>: <span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>+<span>12</span>:<span>00</span>

<span>2022</span>-<span>12</span>-22<span>T22</span>:<span>47</span>:<span>04</span><span>.451</span>+<span>07</span>:<span>00</span>   <span>2022</span>-<span>12</span>-22<span>T15</span>:<span>47</span>:<span>04</span>.451Z e55ee401-87fd-<span>4614</span>-a7c6-971973ccccc6 <span>INFO</span> <span>formatted2</span>: <span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>+<span>12</span>:<span>00</span>
</code>

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:

<code><span>2022</span>-<span>12</span>-22<span>T23</span>:<span>46</span>:<span>32.052</span>+<span>07</span>:<span>00</span>   <span>2022</span>-<span>12</span>-22<span>T16</span>:<span>46</span>:<span>32.</span>052Z f1d04019-<span>44e7</span>-463a-af1d-faf2a5043887 <span>INFO</span> <span>zone_name</span>: <span>Africa</span>/<span>Abidjan</span>

<span>2022</span>-<span>12</span>-22<span>T23</span>:<span>46</span>:<span>32.052</span>+<span>07</span>:<span>00</span>   <span>2022</span>-<span>12</span>-22<span>T16</span>:<span>46</span>:<span>32.</span>052Z f1d04019-<span>44e7</span>-463a-af1d-faf2a5043887 <span>INFO</span> <span>timezone</span>: <span>GMT</span>
</code>

Is that the problem of momentjs, or my code, or lambda? How can I fix it?

moment version: "^2.29.4"

  • Answer
Remain: 5
1 Answer
Avatar
tvd12 Beginner
tvd12 Beginner
I see the final logs was not printed by the first source 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)

Could you provide the source code of the final logs?

  • 0
  • Reply
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

for the zone name

The last log I totally forget :D

 –  anaconda875 1672381731000