-
-
Save aboudard/73eed78c4b64268f2d6863b9e80947c7 to your computer and use it in GitHub Desktop.
Exemple of ngb-date-parser-formatter implementation (ng-bootstrap)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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