Skip to main content Skip to docs navigation

Plugins - Moment

On this page

If you still need to use moment.js, you can load this plugin to use moment to parse input dates.

//example picker
//note that you can optionally provide the format to use.
tempusDominus.extend(tempusDominus.plugins.moment_parse, 'DD.MM.yyyy hh:mm a');
const datetimepicker1 = new tempusDominus.TempusDominus(document.getElementById('datetimepicker1'));

How it works

This plugin overrides two picker functions setFromInput and formatInput.

setFromInput parses and sets a date at the provided index with the textbox value.

formatInput is the reverse, it takes a date time object and formats or parses it to a string.

//obviously, loading moment js is required.
declare var moment;
export const load = (option, tdClasses, tdFactory) => {
  tdClasses.Dates.prototype.setFromInput = function(value, index) {
    let converted = moment(value, option);
    if (converted.isValid()) {
      let date = tdFactory.DateTime.convert(converted.toDate(), this.optionsStore.options.localization.locale);
      this.setValue(date, index);
    }
    else {
      console.warn('Momentjs failed to parse the input date.');
    }
  }

  tdClasses.Dates.prototype.formatInput = function(date) {
    return moment(date).format(option);
  }
}