Hacker0x01 / react-datepicker

A simple and reusable datepicker component for React
https://reactdatepicker.com/
MIT License
8.11k stars 2.24k forks source link

The default date format should be the local date format #3716

Open PabloRomanH opened 2 years ago

PabloRomanH commented 2 years ago

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',
  })
Nestoro commented 2 years ago

It is possible if you supply a dateformat

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

but I agree that it should be default behavior

TitoSniberb commented 1 year ago

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} />
    )
}
github-actions[bot] commented 5 months ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 10 days.

PabloRomanH commented 5 months ago

Any plans to fix this? Really showing the locale date format is the default when using the JS Date object, so doing this is probably a matter of not formatting the date in a specific format.