How to convert new Date() in ECMA Script6 (JavaScript) from UTC to JST (note in AWS Lambda)

How to convert new Date() in ECMA Script6 (JavaScript) from UTC to JST (note in AWS Lambda)

How to convert new Date() in ECMA Script6 (JavaScript) from UTC to JST.

new Date() to UTC time.

let a = new Date();
console.log(a);

Results.

2017-12-02T08:05:52.376Z

“T” for date output format

In the above, the letter “T” appears between the year, month, and hour. This “T” is meant to indicate a break between the year, month, and day and the time.

“Z” for date output format

The above shows the letter “Z” at the end. This means UTC. Others are displayed as “+09:00”.

UTC and JST

JST is simply Japan time; UTC is like Universal Coordinated Time, which is Japan time minus 9 hours.

Convert from UTC to JST

There is no method to get a Date object in JST with new Date(). So we will calculate from UTC and convert it to JST. However, we only need to add 9 hours.

let date = new Date();
date.setTime(date.getTime() + 1000*60*60*9);// Convert to JST
console.log(date);

This will convert the date time to JST.

Be careful when acquiring

Since UTC is only adjusted to JST time, the only methods affected are those with UTC, such as getUTCFullYear(), getUTCDate(), and getUTCHours().

On AWS Lambda, everything is in UTC.

In Lambda(node.js), the return value of getHours() and getUTCHours() are the same. (All other methods) Apparently, everything on AWS returns UTC. If you run the following as index.js with index.handler as the handler, you will see that the results are the same.

exports.handler = (event, context, callback) => {
  let dt = new Date();
  console.log(dt.getFullYear());
  console.log(dt.getUTCFullYear());
  console.log(dt.getUTCDate());
  console.log(dt.getDate());
  console.log(dt.getUTCHours());
  console.log(dt.getHours());
  // TODO implement
  callback(null, 'Hello from Lambda');
};

Change the time zone with environment variables

Lambda in the Tokyo region will also be in UTC time, but you can change the time zone to local by setting the following.

process.env.TZ = 'Asia/Tokyo';

Methods that do not say UTC, such as the getHours() method, will get local time.

Use moment-timezone in Lambda (node.js) *Out of support

Apparently, environment variable override is deprecated, so in Lambda (node.js) you can use moment-timezone to specify the timezone to JST.

const moment = require('moment-timezone');

exports.handler = async (event) => {
  const dateTimeJst = moment().tz('Asia/Tokyo').format();
  console.log(dateTimeJst);// JST
};

To format in ISO8601 format in moment-timezone, specify the following arguments to the format method.
|usage|format|output|
|—|—|—|
|format(‘YYYYYMMDDTHHmmssZ’)|YYYYYMMDDTHHmmssZ|20200624T114138+09:00|
|format(‘YYYYYMMDDTHHmmssz’)|YYYYYMMDDTHHmmssz|20200624T113848JST|
The output differs depending on the case of “z”.

Date large and small comparisons between moments* no longer supported

To make large/small date comparisons in moment-timezone, use isAfter, isBefore, etc.
A date string is passed as the first argument of moment for comparison.
|dateA|dateB|method|true value|
|—|—|—|—|
|20200625T000001+09:00|20200630T000000+09:00|isAfter|false|
|20200630T000000+09:00|20200625T000001+09:00|isAfter|true|
|20200625T000001+09:00|20200630T000000+09:00|isBefore|true|
|20200630T000000+09:00|20200625T000001+09:00|isBefore|false|
Here is an example

moment('20200625T000001+09:00').isBefore(moment('20200630T000000+09:00'))

The current date can be obtained by writing The return value is a string.

moment().tz('Asia/Tokyo').format('yyyyMMDDTHHmmss'); // 20200702T000000+09:00
moment().tz('Asia/Tokyo').format('YYYYMMDDTHHmmss'); // 20200702T075247+09:00

If format is set to yyyyMMDDTHHmmss, the hour, minute, and second will be 00000000, so yyyy must be YYYY.

Check ISO8601 format in moment * no longer supported

Use the isValid method of moment-timezone to check the format validity of a date.

const moment = require('moment-timezone')
let bool
bool = moment('20200101T101010+09:00',moment.ISO_8601).isValid()
console.log(bool) // true
bool = moment('20200101T101010+0900',moment.ISO_8601).isValid()
console.log(bool) // true

moment.js is no longer supported

moment.js is no longer supported and will be in maintenance mode without new development.
https://momentjs.com/docs/#/-project-status/

コメント

タイトルとURLをコピーしました