A literal Z is used in ISO8601 timezone formats. I'd like to use a literal Z if the timezone is UTC when formatting a particular date, but can't seem to make moment-timezone do that. Format string 'YYYY-MM-DD_HH-mm-ssZ' results in 2015-11-11_18-13-14+00:00, 'YYYY-MM-DD_HH-mm-ssZ' results in 2015-11-11_18-13-14+00:00, and 'YYYY-MM-DD_HH-mm-ssZ' results in 2015-11-11_18-13-14UTC.
Perhaps 'ZZZ' should be added, which formats a literal, single Z if timezone is UTC, otherwise it would format the same as 'ZZ', such that 'YYYY-MM-DD_HH-mm-ssZZZ' results in 2015-11-11_18-13-14Z.
Can you just use moment.toISOString() ?
No because the format I need is different than that. See my example.
On Thursday, May 7, 2015, Matt Johnson [email protected] wrote:
Can you just use moment.toISOString() ?
—
Reply to this email directly or view it on GitHub
https://github.com/moment/moment-timezone/issues/213#issuecomment-99925538
.
mailto:[email protected] [email protected]
skype:matthewadams12
googletalk:[email protected]
http://matthewadams.me
http://www.linkedin.com/in/matthewadams
Just put the Z in brackets and force the UTC zone. You don't even need moment-timezone for this, moment will do it just fine on its own.
m.utc().format("YYYY-MM-DD_HH-mm-ss[Z]")
Or if what you're saying is that you'd like the Z only when the offset is _already_ zero, but _not_ for other offset, then just do something like this:
m.format("YYYY-MM-DD_HH-mm-ss" + (m.utcOffset() === 0 ? '[Z]' : 'Z'))
Do keep in mind though that there is a subtle difference in the context between +00:00 and Z. Both are allowed under ISO8601, but the former says that the timestamp could be a local time that has no delta from UTC at that particular moment, while Z indicates that the value is explicitly stated in terms of UTC. In most cases, the distinction is too subtle to matter. And the rest of your format isn't ISO8601 compliant anyway. :wink:
Understood. I'll still think there's a case to be for ZZZ to be syntax
sugar to mean precisely (m.utcOffset() === 0 ? '[Z]' : 'Z'), so I don't
have to arduously type the latter where I need it.
And the rest of your format isn't ISO8601 compliant anyway. :wink:
I know it's not; I never claimed that it was. I just needed 'Z' instead of
'UTC' or '+00:00' or '+0000'.
Opened an issue to track this in moment, as it's not related to moment-timezone. Thanks for the idea!
We can use below format of utc to format utc date -
let date = moment.utc(moment("/Date(1198908717056-0700)/").toString()).format("YYYY-MM-DD")
@vaishnavravi33 - That's not recommended, and is not what this issue was about anyway.
Just put the Z in brackets and force the UTC zone. You don't even need moment-timezone for this, moment will do it just fine on its own.
m.utc().format("YYYY-MM-DD_HH-mm-ss[Z]")Or if what you're saying is that you'd like the
Zonly when the offset is _already_ zero, but _not_ for other offset, then just do something like this:m.format("YYYY-MM-DD_HH-mm-ss" + (m.utcOffset() === 0 ? '[Z]' : 'Z'))Do keep in mind though that there is a subtle difference in the context between
+00:00andZ. Both are allowed under ISO8601, but the former says that the timestamp could be a local time that has no delta from UTC at that particular moment, whileZindicates that the value is explicitly stated in terms of UTC. In most cases, the distinction is too subtle to matter. And the rest of your format isn't ISO8601 compliant anyway.
Saved my day, Sir!
What if this one?
moment('20200324T102042').utc().format('YYYYMMDDTHHmmss[Z]')
"20200324T045042Z"
Most helpful comment
What if this one?
moment('20200324T102042').utc().format('YYYYMMDDTHHmmss[Z]')"20200324T045042Z"