1

I'm currently formatting a date in my component using the date pipe:

this.datePipe.transform(myDate, 'M/d/yy H:mm z');

The resulting output looks like this: 2/6/20 11:59 GMT-5

Is there a way to format the date like this: 2/6/20 11:59 GMT without resorting to string manipulation or a library?

The Angular date pipe uses the timezone from the client machine, and I would like to retain that functionality.

ChekTek
  • 175
  • 2
  • 10
  • Do you simply want to hide the offset, or also convert the date to GMT and hide the offset? Edit: Cause it'd be very confusing if you display GMT when it's actually GMT-5. – alexania Feb 06 '20 at 18:15
  • 1
    It is a user requirement to hide the offset (in my example "-5"). Unfortunately it is not my decision. – ChekTek Feb 06 '20 at 20:01
  • The project uses moment-timezone elsewhere, so I will use https://momentjs.com/timezone/ for now, although I think this would be easy enough to add as a feature for Angular's DatePipe format string. – ChekTek Feb 06 '20 at 21:11

1 Answers1

0

If you simply want to hide the offset and always display GMT for whatever reason, you could just hard code it in instead of using z.

this.datePipe.transform(myDate, 'M/d/yy H:mm \'GMT\'');
alexania
  • 2,395
  • 2
  • 13
  • 11
  • The angular date pipe uses the timezone of the client machine, I'd like to retain that functionality rather than hardcode the value. – ChekTek Feb 06 '20 at 20:02
  • But the "timezone of the client machine" is just going to be "GMT" plus the offset, so you gain absolutely nothing by using it and then chopping off the offset. Unless I'm missing something? – alexania Feb 06 '20 at 20:53
  • As state in the comment above, it is a user requirement for display purposes, unfortunately it is not my decision. – ChekTek Feb 06 '20 at 20:57