1

HTML code

 <form novalidate [formGroup]="udpateObj">
    <div class="form-group">
      <label for="HDresolutionDate">Resolution Date:</label>
      <input type="text" id="date"  formControlName="DateP" ngbDatepicker
                                    #d1="ngbDatepicker">   

    </div>
  <form>

code in ts file

let dateVal  = backendDate;// am getting date obj from backend and 
  this.udpateObj= new FormGroup({ 
  DateP: new FormControl(dateVal)
});

I need to show the date which is coming from backend or selected from date picker in dd/mm/yyyy format.

2
  • there must be option for the format of date in the datepicker you have used. if there is nothing then you have to change manually forma of date using javascript Commented Dec 27, 2016 at 11:18
  • is there a reason you wouldn't use NgbDateParserFormatter? Per their docs. ng-bootstrap.github.io/#/components/datepicker Commented Dec 27, 2016 at 21:31

1 Answer 1

2

You can use the date pipe like in this example:

<input type="text" value = "{{dateObject | myDateFormat: 'dd/mm/yyyy' }}/> 

or implement a custom pipe if selected date is not a Date Object. Find here example for custom pipe (using momentjs) applying the format passed in call or the default format 'HH:mm'

import { Pipe, PipeTransform} from '@angular/core';
declare var moment: any;

@Pipe({
  name: 'myDateFormat'
})
export class myDateFormat implements PipeTransform {
   transform(value: any, args: string[]): any {
     if (value) {
         var date = value instanceof Date ? value : moment(value);
         if (args && args.length > 0)
             return moment(date).format(args);
         else 
            return moment(date).format('HH:mm');
    }
  }
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.