Take as an example the following minimal reproducible example of a project I'm working on where I receive a timezone name and I need to convert it to a "human-readable format".
i.e.:
America/Los_Angeles
America Los Angeles
I now, you may consider "America/Los_Angeles" is already "human-readable" and I agree, but it's a requirement of my issue to convert it to the mentioned format (or similar, without including slashes, underscores, etc).
Also, this could be achieved easily using a regular expression, but as moment and moment timezone are being used as part of the project... I wonder, is there a way to do achieve this using any built-in mechanism provided by any of these libraries?
import 'moment-timezone';
import * as moment from 'moment';
const tz = moment.tz.zone("America/Los_Angeles");
const result = tz.name;
console.log(result); // America/Los_Angeles, need "America Los Angeles"
If it is of any help, this is in an Angular project.
This is addressed in the docs on formatting:
Moment.js also provides a hook for the long form time zone name. Because these strings are generally localized, Moment Timezone does not provide any long names for zones.
To provide long form names, you can override
moment.fn.zoneName
and use thezz
token.
var abbrs = {
EST : 'Eastern Standard Time',
EDT : 'Eastern Daylight Time',
CST : 'Central Standard Time',
CDT : 'Central Daylight Time',
MST : 'Mountain Standard Time',
MDT : 'Mountain Daylight Time',
PST : 'Pacific Standard Time',
PDT : 'Pacific Daylight Time',
};
moment.fn.zoneName = function () {
var abbr = this.zoneAbbr();
return abbrs[abbr] || abbr;
};
moment.tz([2012, 0], 'America/New_York').format('zz'); // Eastern Standard Time
moment.tz([2012, 5], 'America/New_York').format('zz'); // Eastern Daylight Time
moment.tz([2012, 0], 'America/Los_Angeles').format('zz'); // Pacific Standard Time
moment.tz([2012, 5], 'America/Los_Angeles').format('zz'); // Pacific Daylight Time