Skip to content

Instantly share code, notes, and snippets.

@aboudard
Forked from nrobinaubertin/app.component.ts
Last active June 26, 2018 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aboudard/73eed78c4b64268f2d6863b9e80947c7 to your computer and use it in GitHub Desktop.
Save aboudard/73eed78c4b64268f2d6863b9e80947c7 to your computer and use it in GitHub Desktop.
Exemple of ngb-date-parser-formatter implementation (ng-bootstrap)
import { NgbDatepickerConfig, NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
import { NgbDateFRParserFormatter } from "./ngb-date-fr-parser-formatter"
@Component({
providers: [{provide: NgbDateParserFormatter, useClass: NgbDateFRParserFormatter}]
})
export class AppComponent {}
import { NgbDateStruct } from "@ng-bootstrap/ng-bootstrap";
export static class DateUtil {
public static ngbDateStructToDate(dateStruct : NgbDateStruct) : Date {
if(!dateStruct) {
return null;
}
return new Date(dateStruct.year, dateStruct.month - 1, dateStruct.day);
}
}
import { Injectable } from "@angular/core";
import { NgbDateParserFormatter, NgbDateStruct } from "@ng-bootstrap/ng-bootstrap";
import * as moment from "moment";
import {Parameters} from "../../app.parameter";
import {DateUtil} from "../../utils/date.util";
@Injectable()
export class NgbDateFRParserFormatter extends NgbDateParserFormatter {
parse(value: string): NgbDateStruct {
if (value && moment.isDate(value)) {
const date = moment(value).toDate();
return {
year : date.getFullYear(),
month : date.getMonth() + 1,
day : date.getDate()
};
}
return null;
}
format(date: NgbDateStruct): string {
return date && moment(DateUtil.ngbDateStructToDate(date)).format(Parameters.defaultDisplayDatePattern);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment