Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Date to String into the format yyyy-mm-dd in typescript [duplicate]

How to convert Date to String into the format yyyy-mm-dd in typescript

Now I am getting the date as Fri Mar 24 2017 00:00:00 GMT-0400 (Eastern Daylight Time)

I only need date in the format "2017-03-24" from that and without any timezones and timezone conversions

like image 791
Shiny Avatar asked Oct 22 '25 06:10

Shiny


1 Answers

Assuming you are using Angular, you may import DatePipe, or FormatDate into your component.ts to handle that. You may read more about the various date/time formats you can pass as the parameters.

1) DatePipe:

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

export class SampleComponent implements OnInit {   

constructor(private datePipe: DatePipe) {}

  transformDate(date) {
    return this.datePipe.transform(date, 'yyyy-MM-dd');
  }
}

Do not forget to add DatePipe to your providers in your module.

providers: [DatePipe]

2) formatDate:

import { formatDate } from '@angular/common';

export class SampleComponent implements OnInit {

  constructor(@Inject(LOCALE_ID) private locale: string) {}

  transformDate(date) {
    return formatDate(date, 'yyyy-MM-dd', this.locale);
  }
}
like image 148
wentjun Avatar answered Oct 23 '25 21:10

wentjun



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!