Search code examples
reactjsreact-datepickerdate-fns

How to make react-datepicker start the days of the week on Monday?


I'm using react-datepicker along with date-fns to display a date picker with Hungarian locale in my Web app, but I couldn't find a way for it to display the dates of the week starting with Monday instead of Sunday as per Hungarian conventions. The datepicker looks like this (V is short for Vasárnap, which of course means Sunday):

react-datepicker with Hungarian locale

Here is the code the code I used to render it:

import DatePicker, { registerLocale } from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import hu from "date-fns/locale/hu";

registerLocale("hu", hu);

export function CustomDatepicker(props) {
    return (
        <DatePicker className="form-control" selected={props.date} 
        onChange={ props.onChange } dropdownMode="select"
        dateFormat="yyyy.MM.dd." todayButton="Ma" closeOnScroll={true}
        locale="hu" />
    );
}

One idea that I also tried to make this work is the following code (with a modification of the number 0 to 1) that I found in this GitHub issue:

registerLocale("hu", { ...hu, options: { ...hu.options, weekStartsOn: 1 } });

Is there any other way to make this work or should I use a different package for localization?


Solution

  • All you need to pass an additional prop (calendarStartDay={1}) to DatePicker

    import DatePicker, { registerLocale } from "react-datepicker";
    import "react-datepicker/dist/react-datepicker.css";
    import hu from "date-fns/locale/hu";
    
    registerLocale('hu', hu);
    function App(props) {
      return (
        <div className="App">
          <DatePicker
            className="form-control"
            selected={props.date}
            onChange={props.onChange}
            dropdownMode="select"
            dateFormat="yyyy.MM.dd."
            todayButton="Ma"
            closeOnScroll={true}
            locale="hu"
           calendarStartDay={1}
          />
        </div>
      );
    }
    
    export default App;
    
    

    enter image description here