1

I am using moment js to get only the UTC time.

const time = moment().utc().format('HH:mm:ss');

example output is like this - "07:57:49"

I want to change this time value to relevant timezone value.

const americaTime = moment(time).tz("America/Los_Angeles").format();

But there is an warning saying - "Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Is there any alternative way to overcome this issue ?

Rishan.RM
  • 43
  • 4

3 Answers3

1

do this

let time = moment().utc().tz("America/Los_Angeles").format('HH:mm:ss');
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
0
const time = moment().utc().format('HH:mm:ss');

moment(time,'HH:mm:ss').tz("America/Los_Angeles").format();

You need to specify the format HH:mm:ss in the second param, momentjs throws this error because it is unable to parse the input without the format!

0

You can also display the time UTC, or in any IANA timezone with vanilla JS, using Date.toLocaleTimeString():

let dt = new Date();

const timeZones = ['UTC', 'America/Los_Angeles', 'America/New_York'];

console.log('Time zone'.padEnd(20), 'Current time')
for(let timeZone of timeZones) {
    console.log(timeZone.padEnd(20), dt.toLocaleTimeString('en', {  timeZone, hourCycle: 'h24', hour: '2-digit', minute: '2-digit', second: '2-digit' }));
}
   
.as-console-wrapper { max-height: 100% !important; }
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40