I want to convert current data into 'yyyy-MM-dd'
format in .ts file. i template it can easily be done by using data pipe. how to do that in typescript.
In template:
{{date | date:'yyyy-MM-dd'}}
How to convert in this format 'yyyy-MM-dd'
in typescript.
now i am just getting current date by using this.
this.date = new Date();
but need to convert it into given format. Please guide how to do that... Thanks!
Angular date pipe used to format dates in angular according to the given date formats,timezone and country locale information. Using date pipe, we can convert a date object, a number (milliseconds from UTC) or an ISO date strings according to given predefined angular date formats or custom angular date formats.
You have to pass the locale string as an argument to DatePipe. var ddMMyyyy = this. datePipe. transform(new Date(),"dd-MM-yyyy");
The date can be converted in typescript to this format 'yyyy-MM-dd'
by using Datepipe
import { DatePipe } from '@angular/common' ... constructor(public datepipe: DatePipe){} ... myFunction(){ this.date=new Date(); let latest_date =this.datepipe.transform(this.date, 'yyyy-MM-dd'); }
and just add Datepipe in 'providers' array of app.module.ts. Like this:
import { DatePipe } from '@angular/common' ... providers: [DatePipe]
The same problem I faced in my project. Thanks to @Umar Rashed, but I am going to explain it in detail.
First, Provide Date Pipe from app.module:
providers: [DatePipe]
Import to your component and app.module:
import { DatePipe } from '@angular/common';
Second, declare it under the constructor:
constructor( public datepipe: DatePipe ) {
Dates come from the server and parsed to console like this:
2000-09-19T00:00:00
I convert the date to how I need with this code; in TypeScript:
this.datepipe.transform(this.birthDate, 'dd/MM/yyyy')
Show from HTML template:
{{ user.birthDate }}
and it is seen like this:
19/09/2000
also seen on the web site like this: dates shown as it is filtered (click to see the screenshot)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With