moment-timezone
moment-timezone copied to clipboard
TypeError: Cannot read property 'format' of undefined
I have a code like this :
const T = moment().tz(process.env.TZ).format('YYYY-MM-DD');
which was being converted by babel to ES5 with the output like :
var _momentTimezone = require('moment-timezone');
var _momentTimezone2 = _interopRequireDefault(_momentTimezone);
const T = (0, _momentTimezone2.default)().tz(process.env.TZ).format('YYYY-MM-DD');
Error
TypeError: Cannot read property 'format' of undefined
What is process.env.TZ in this case? If it is undefined (or null, ``, etc.) it will produce this result.
If you're just looking for the local time zone, you probably don't want to guess that it will be well-formed and in the TZ environment variable. That's not a guarantee on most systems. A better approach is to just use moment's local-time functionality, which is the default. You don't need moment-timezone for that. In other words, you'd have the same effect with:
const T = moment().format('YYYY-MM-DD');
What is process.env.TZ in this case? If it is undefined (or null, ``, etc.) it will produce this result.
What is the motivation for this behavior?