Skip to content
Advertisement

Format current date time to “yyyy-MM-dd’T’HH:mm:ss.SSSZ” in Angular/Javascript/Typescript

Is there an easy way to format the date time in Angular/Typescript in “yyyy-MM-dd’T’HH:mm:ss.SSSZ”

I got the current date by doing

JavaScript

Can you please tell me what do I need to add here in the method to get in format of yyyy-MM-dd’T’HH:mm:ss.SSSZ

Advertisement

Answer

There are two ways to achieve this:

  1. The Angular way using DatePipe

In you component, you can inject DatePipe from @angular/common and call on the transform method to format date. Here is the list from Angular docs of options you can use to format date and time.

JavaScript

In the module, where you have defined this component, you need to provide DatPipe as providers

JavaScript
  1. Second way would be to use native toISOString() method
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement