3

I am using vaadin-date-picker to display the date. It's default format is 6/21/2017. I want to show only month and year. Such as jun 2017 or 6/2017. Any help please?

cfrick
  • 35,203
  • 6
  • 56
  • 68
Hassam
  • 31
  • 3

1 Answers1

2

vaadin-date-picker is using i18n property to localize everything. When declaring vaadin-date-picker element, you can also set i18n property. for Example:

<vaadin-date-picker i18n='[[i18nCustom]]'></vaadin-date-picker>

and then declare property:

i18nCustom: {
    value: function() {
        return  {

      week: 'viikko',
      calendar: 'kalenteri',
      clear: 'tyhjennä',
      today: 'tänään',
      cancel: 'peruuta',
      firstDayOfWeek: 1,
      monthNames: ['tammikuu','helmikuu','maaliskuu','huhtikuu','toukokuu','kesäkuu',
                   'heinäkuu','elokuu','syyskuu','lokakuu','marraskuu','joulukuu'],
      weekdays: 'sunnuntai_maanantai_tiistai_keskiviikko_torstai_perjantai_lauantai'.split('_'),
      weekdaysShort: ['su','ma','ti','ke','to','pe','la'],
      formatDate: function(d) {
        return [d.getMonth() + 1, d.getFullYear()].join('/');
      },
      parseDate: function(text) {
        // This example produces a really strict parser which only accepts
        // perfectly formatted dates like '12.8.2013'. Less strict implementation or
        // a 3rd party parser like in the example below is recommended.
        var parts = text.split('.');
        if (parts.length === 3) {
          var date = new Date(0, 0);
          date.setFullYear(parseInt(parts[2]));
          date.setMonth(parseInt(parts[1]) - 1);
          date.setDate(parseInt(parts[0]));
          return date;
        }
      },
      formatTitle: function(monthName, fullYear) {
        return monthName + ' ' + fullYear;
      }
        }
    }
}

What you are looking for is function formatDate. Where you can edit what should be returned as displayed text

Kuba Šimonovský
  • 2,013
  • 2
  • 17
  • 35
  • Wouldn't be a problem if you start the weekdays with Sunday, but the sort versions on Monday? `weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], weekdaysShort: ["Mo", "Th", "We", "Th", "Fr", "Sa", "Su"],` – Márton Apr 24 '17 at 09:16
  • I have never realized i have bug in this. But i am using it for few months in production and no problems were found yet. – Kuba Šimonovský Apr 24 '17 at 09:29
  • its giving error Uncaught TypeError: Cannot read property '1' of undefined – Hassam Apr 24 '17 at 09:59
  • @Hassam try it now, if it's still throwing some error, try to update to newest version. This code is grabbed frm official documentation so it should work. Anyway, you can try it on yourself now, when you know how to do it. Just editing function formatDate is important – Kuba Šimonovský Apr 24 '17 at 10:42