react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

The default date format should be the local date format

Open PabloRomanH opened this issue 2 years ago • 4 comments

Right now the date format that appears in the picker is 'MM/dd/yyyy' instead of the local date format set up in the OS.

Instead, the local date format should be used by default. Right now it's not only not the default, but it's not possible to set it up without installing libraries with big external lists of locales that are prone to errors depending on how each browser and operating system represents the names of the locales.

The local short date format padded with zeros can be easily obtained:

date.toLocaleDateString(undefined, {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
  })

PabloRomanH avatar Aug 04 '22 09:08 PabloRomanH

It is possible if you supply a dateformat

        <RDatePicker
            locale={'de'}
            dateFormat={'dd.MM.yyyy'}
        />

but I agree that it should be default behavior

Nestoro avatar Sep 01 '22 13:09 Nestoro

It is possible if you supply a dateformat

        <RDatePicker
            locale={'de'}
            dateFormat={'dd.MM.yyyy'}
        />

but I agree that it should be default behavior

That's some bs, but it works this way. I needed it dynamic but whatever, the library is still good enough. What I did to make it work while being dynamic:

import { es, enUS, fr } from 'date-fns/locale';
import { default as ReactDatePicker, registerLocale, setDefaultLocale } from 'react-datepicker';

const DatePicker= ({ locale, ... }) => {
    const retardDateFormat = 'MM/dd/yyyy' as const;
    const nonRetardDateFormat = 'dd/MM/yyyy' as const;
    const locales = {
      es: {
        locale: es,
        format: nonRetardDateFormat,
      },
      en: {
        locale: enUS,
        format: retardDateFormat,
      },
      fr: {
        locale: fr,
        format: nonRetardDateFormat,
      },
    };

    return (
       <ReactDatePicker locale={locale} dateFormat={locales[locale].format} />
    )
}

TitoSniberb avatar Mar 31 '23 12:03 TitoSniberb