question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alternative Date adapters

See original GitHub issue

Now that we have an NgbDateAdapter, it might be nice to bundle a couple of default ones with the library:

  • NgbDateNativeAdapter - as seen in the datepicker adapter demo to use with native JS date
  • NgbDateISOAdapter - to conform with ISO 8601 Dates
  • NgbDateMomentAdapter - for use with moment js

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
tech-no-logicalcommented, Sep 10, 2018

my super simple ISO implementation, fwiw :

function padNumber(value: number) {
  if (isNumber(value)) {
    return `0${value}`.slice(-2);
  } else {
    return '';
  }
}

@Injectable()
export class NgbIsoDateAdapter extends NgbDateAdapter<string> {
  fromModel(date: string): NgbDateStruct {

    const parsedDate = /(\d\d\d\d)-(\d\d)-(\d\d)/.exec(date);
    if (parsedDate) {
      return <NgbDateStruct>{ year: Number(parsedDate[1]), month: Number(parsedDate[2]), day: Number(parsedDate[3]) };
    } else {
      return null;
    }
  }
  toModel(date: NgbDateStruct): string {
    if (date) {
      return date.year + '-' + padNumber(date.month) + '-' + padNumber(date.day);
    } else {
      return null;
    }
  }
}
2reactions
jeroenheijmanscommented, Jun 5, 2018

For what it’s worth, I’ve just crafted a Moment.js adapter with a few basic smoke tests. The full deal is in a public gist, here’s the adapter bits:

// Might need to polyfill depending on needed browser support...
const isInt = Number.isInteger;

@Injectable()
export class NgbMomentjsAdapter extends NgbDateAdapter<moment.Moment> {

  fromModel(date: moment.Moment): NgbDateStruct {
    if (!date || !moment.isMoment(date)) {
      return null;
    }

    return {
      year: date.year(),
      month: date.month() + 1,
      day: date.date(),
    };
  }

  toModel(date: NgbDateStruct): moment.Moment {
    if (!date || !isInt(date.day) || !isInt(date.day) || !isInt(date.day)) {
      return null;
    }

    return moment(`${date.year}-${date.month}-${date.day}`, 'YYYY-MM-DD');
  }
}

I think there’s a util based version of isInt in the library itself that might be useful instead of Number.isInteger.

But, at the bottom line, I guess @maxokorokov has a point that it’s not so nice to pull in Moment.js as a dependency just to demonstrate something. Not sure what the solution would be for that (if any).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Birthday Adapter Alternatives and Similar Apps - AlternativeTo
There are four alternatives to Birthday Adapter for Android and F-Droid. The best alternative is Birday, which is both free and Open Source....
Read more >
Datepicker - Angular powered Bootstrap
Bootstrap widgets for Angular: autocomplete, accordion, alert, carousel, datepicker, dropdown, offcanvas, pagination, popover, progressbar, rating, tabset, ...
Read more >
date-fns - modern JavaScript date utility library
date -fns provides the most comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
Read more >
SportRack Alternative Bike Adapter - Amazon.com
The SportRack Adjustrable Bike Adapter is the perfect solution to carry bikes with uniquely shaped frames on a rear-mounted hanging bike rack.
Read more >
Still using java.util.Date? Don't! - Programming Hints
time API out of the box. But there are already different adapters implementations, to support marshalling/unmarshalling of new java.time classes. It does ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found