2

I have bsDatepicker with bsConfig and I'm trying to validate it, to make sure it is with the right format. The problem is that bsDatepicker create a date in a different format so I can't validate it.

The date string is:

Mon Apr 01 2019 19:05:36 GMT+1100 (Australian Eastern Daylight Time)

I'm trying to transform it using DatePipe like:

dateStr = this.datepipe.transform(date.toDateString(), 'MM/dd/yyyy');

What I'm getting is null. The expected result will be 04/01/2019. Any idea why the datepipe failed to transform? Thanks.

user2304483
  • 1,462
  • 6
  • 28
  • 50

4 Answers4

2
private static datePipeEn: DatePipe = new DatePipe('en-US');

Solved my problem, I hope it will help someone with similar issue!

user2304483
  • 1,462
  • 6
  • 28
  • 50
1

Not sure why toDateString() to that time. You can parse to Date like this

this.dateStr = this.datePipe.transform(new Date(date.toDateString()) ,'MM/dd/yyyy');

Demo: https://stackblitz.com/edit/angular-datepipe-in-component-test

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

date.toDateString you are missing parentheses date.toDateString()

onik
  • 1,579
  • 1
  • 11
  • 19
0

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, 'mm-dd-yyyy');
}

and just add Datepipe in 'providers' array of app.module.ts. Like this:

import { DatePipe } from '@angular/common'

providers: [DatePipe]