Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert time to this format YYYY-MM-DDThh:mm:ssTZD in Angular?

We are using java as Backend ,in which we use the ISO 8601. To pass the the same I need to convert the date into same format.

in java we use

DateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

similarly how to convert the date into the very same format in Angular/TypeScript

I'm attaching an Image for your reference for the date format. please see the image.

enter image description here

like image 446
AMAL MOHAN N Avatar asked Oct 20 '25 22:10

AMAL MOHAN N


1 Answers

You need to use DatePipe in your component

app.component.ts

import { Component } from '@angular/core';
import { DatePipe } from '@angular/common';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  providers: [DatePipe]
})
export class AppComponent  {
  today;
  constructor(
    private datePipe:DatePipe
  ){
      this.today = this.datePipe.transform( new Date(),'yyyy-MM-dd  h:mm:ssZZZZZ');
  }
}

For any other date-formats check here: https://angular.io/api/common/DatePipe

Working stackblitz example: https://stackblitz.com/edit/angular-datepipe-in-component-u8bzre?file=src/app/app.component.ts

like image 137
Msk Satheesh Avatar answered Oct 23 '25 13:10

Msk Satheesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!