Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { NgbDateParserFormatter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
  3.  
  4. @Injectable()
  5. export class NgbDateUKParserFormatter extends NgbDateParserFormatter {
  6. parse(value: string): NgbDateStruct {
  7. if (value) {
  8. const dateParts = value.trim().split('/');
  9. if (dateParts.length === 1 && isNumber(dateParts[0])) {
  10. return {year: toInteger(dateParts[0]), month: null, day: null};
  11. } else if (dateParts.length === 2 && isNumber(dateParts[0]) && isNumber(dateParts[1])) {
  12. return {year: toInteger(dateParts[1]), month: toInteger(dateParts[0]), day: null};
  13. } else if (dateParts.length === 3 && isNumber(dateParts[0]) && isNumber(dateParts[1]) && isNumber(dateParts[2])) {
  14. return {year: toInteger(dateParts[2]), month: toInteger(dateParts[1]), day: toInteger(dateParts[0])};
  15. }
  16. }
  17. return null;
  18. }
  19.  
  20. format(date: NgbDateStruct): string {
  21. let stringDate = '';
  22. if (date) {
  23. stringDate += isNumber(date.day) ? padNumber(date.day) + '/' : '';
  24. stringDate += isNumber(date.month) ? padNumber(date.month) + '/' : '';
  25. stringDate += date.year;
  26. }
  27. return stringDate;
  28. }
  29. }
  30.  
  31. function padNumber(value: number) {
  32. if (isNumber(value)) {
  33. return `0${value}`.slice(-2);
  34. } else {
  35. return '';
  36. }
  37. }
  38.  
  39. function isNumber(value: any): boolean {
  40. return !isNaN(toInteger(value));
  41. }
  42.  
  43. function toInteger(value: any): number {
  44. return parseInt(`${value}`, 10);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement