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



“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”.


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

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();
  // 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.
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|
Here is an example


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.


Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL