moment-timezone icon indicating copy to clipboard operation
moment-timezone copied to clipboard

TypeError: Cannot read property 'format' of undefined

Open jofftiquez opened this issue 8 years ago • 3 comments

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

jofftiquez avatar Oct 04 '17 14:10 jofftiquez

What is process.env.TZ in this case? If it is undefined (or null, ``, etc.) it will produce this result.

mattjohnsonpint avatar Nov 01 '17 20:11 mattjohnsonpint

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

mattjohnsonpint avatar Nov 01 '17 20:11 mattjohnsonpint

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?

DCtheTall avatar Dec 08 '17 20:12 DCtheTall