4

HTML:-

      <li><span class="float-left label">Date of birth</span>
        <div class="float-right fill-text position-relative edit-calender">
          <input type="text" (click)="d.toggle()" class="edit-field"
            [outsideDays]="'collapsed'" id="datePicker" ngbDatepicker formControlName="dob" #d="ngbDatepicker" [minDate]="minDate"
            [maxDate]="maxDate" readonly>
          <span class="calendar" id="calIcon" (click)="waitAndToggle();"><i id="calIcon" class="far fa-calendar-alt"></i></span>
          <div class="text-danger" *ngIf="form.get('dob').hasError('required')
                && ( form.get('dob').touched || formSubmitAttempt)">
            {{ 'Validations.dob' | translate }}
          </div>
        </div>
      </li>

Now when I select some date, it set as YYYY-MM-DD format but I need to change in other format (MM-DD-YYYY or DD-MM-YYY). Check its node_modules folder but didn't git any solution for the same.

I checked the gist https://gist.github.com/nrobinaubertin/61ff1c3db355c74f4e56f485b566ab22 but this didn't set the value in input field as DD-MM-YYYY.

2 Answers 2

5

To use a class that extends a NbgDateParserFormat you must include it in app.module in providers,

@NgModule({
  declarations: [
    AppComponent,
    ...  
  ],
  imports: [
    BrowserModule,
    NgbModule,
    ...
  ],
  providers: [
    { provide: NgbDateParserFormatter, useClass: DateParserFormatter },
    ...

  ],
  bootstrap: [AppComponent]
})
Sign up to request clarification or add additional context in comments.

4 Comments

This works in your link. Let me implement in my code
Also work in mine too but I m surprised to see we need to do lots of changes for change only format
Really it's only write a DateParserFormatter (the example is more complex because I wanted the class was "parametrizable", but it's only create two functions: "parse" and "format")
Just wanted to thank you for this, I finally got it working in my app. :)
0

The perfect solution would be to change

ngb-date-parser-formatter.js

from the code below you will get DD-MM-YYYY Date format in input filed.

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { padNumber, toInteger, isNumber } from '../util/util';
/**
 * Abstract type serving as a DI token for the service parsing and formatting dates for the NgbInputDatepicker
 * directive. A default implementation using the ISO 8601 format is provided, but you can provide another implementation
 * to use an alternative format.
 */
var NgbDateParserFormatter = (function () {
    function NgbDateParserFormatter() {
    }
    return NgbDateParserFormatter;
}());
export { NgbDateParserFormatter };
var NgbDateISOParserFormatter = (function (_super) {
    __extends(NgbDateISOParserFormatter, _super);
    function NgbDateISOParserFormatter() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    NgbDateISOParserFormatter.prototype.parse = function (value) {
        if (value) {
            var dateParts = value.trim().split('-');
            if (dateParts.length === 1 && isNumber(dateParts[0])) {
                return { year: toInteger(dateParts[0]), month: null, day: null };
            }
            else if (dateParts.length === 2 && isNumber(dateParts[0]) && isNumber(dateParts[1])) {
                return { year: toInteger(dateParts[0]), month: toInteger(dateParts[1]), day: null };
            }
            else if (dateParts.length === 3 && isNumber(dateParts[0]) && isNumber(dateParts[1]) && isNumber(dateParts[2])) {
                return { year: toInteger(dateParts[0]), month: toInteger(dateParts[1]), day: toInteger(dateParts[2]) };
            }
        }
        return null;
    };
    NgbDateISOParserFormatter.prototype.format = function (date) {
        return date ?
        (isNumber(date.day) ? padNumber(date.day) : '')+"-"+ (isNumber(date.month) ? padNumber(date.month) : '') +"-"+ date.year:'';
    };
    return NgbDateISOParserFormatter;
}(NgbDateParserFormatter));
export { NgbDateISOParserFormatter };
//# sourceMappingURL=ngb-date-parser-formatter.js.map

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.