SHARE
TWEET

Untitled

a guest Oct 14th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { NgbDateAdapter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
  2. import { Injectable } from '@angular/core';
  3. import * as moment from 'moment';
  4.  
  5. // Might need to polyfill depending on needed browser support...
  6. const isInt = Number.isInteger;
  7.  
  8. @Injectable()
  9. export class NgbMomentjsAdapter extends NgbDateAdapter<moment.Moment> {
  10.  
  11.   fromModel(date: moment.Moment): NgbDateStruct {
  12.     if (!date || !moment.isMoment(date)) {
  13.       return null;
  14.     }
  15.  
  16.     return {
  17.       year: date.year(),
  18.       month: date.month() + 1,
  19.       day: date.date(),
  20.     };
  21.   }
  22.  
  23.   toModel(date: NgbDateStruct): moment.Moment {
  24.     if (!date || !isInt(date.year) || !isInt(date.month) || !isInt(date.day)) {
  25.       return null;
  26.     }
  27.  
  28.     return moment(`${date.year}-${date.month}-${date.day}`, 'YYYY-MM-DD');
  29.   }
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top