Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the format for react datepicker days of the week?

I am using React Datepicker and all is working great.

Instead of having "Su" "Mo" "Tu" etc. for days of the week, I'd like to use something like this:

"Sun" "Mon" "Tue" 

I am accomplishing this via css:

.react-datepicker__day-name {
    ...
    &:first-child {
        &:after {
            visibility: visible;
            position: relative;
            left: -0.5rem;
            text-align: center;
            content: "Sun";
        }
    }
}

It seems to work, but also seems pretty "hacky". Is there a configuration option that I'm missing somewhere that I can specify what format(s) I'd like to use?

Thank you for any suggestions!

EDIT

Here is how I am (trying) to pass the dateFormat to my component:

App.js

this.state = {
    startDate: new Date(),
    dateFormat: 'ddd'
};


...

render() {
    ...
     <ReservationCalendar
         dateFormat={ this.state.dateFormat }
         startDate={ this.state.startDate  }
     />
}

ReservationCalendar.js

const ReservationCalendar = ({dateFormat, startDate}) => (
    <Calendar
        dateFormat={ dateFormat }
        startDate={ startDate }/>
);

export default ReservationCalendar;

DatePicker.js

const Calendar = ({dateFormat, startDate}) => (
    <div className="my-calendar">
        <DatePicker
            inline
            dateFormat={ dateFormat }
            selected={ startDate }
        />
    </div>
);

export default Calendar;

With everything like that, I'm still getting a view like i've attached. Maybe I'm not passing something through correctly. Thank you again for your time!

enter image description here

like image 291
Damon Avatar asked Jun 14 '19 17:06

Damon


People also ask

How do I change datepicker format in react?

Date format is a way of representing the date value in different string format in the textbox. By default, the DatePicker's format is based on the culture. You can also set the own custom format by using the format property.

How do I change the date format in react native?

In your component: import { format } from "date-fns"; var date = new Date("2016-01-04 10:34:23"); var formattedDate = format(date, "MMMM do, yyyy H:mma"); console. log(formattedDate); Substitute the format string above "MMMM do, yyyy H:mma" with whatever format you require.


2 Answers

Needed something similar at work recently. This is how I was able to do it using formatWeekDay prop:

<DatePicker
  formatWeekDay={nameOfDay => nameOfDay.substr(0,3)}
/>;

calendar screenshot

Note: Couldn't find this prop in public facing docs though.

like image 85
Manish Avatar answered Oct 23 '22 01:10

Manish


You can use useWeekdaysShort attribute of react-datepicker.

e.g:

<DatePicker>
useWeekdaysShort={true}
</DatePicker>
like image 34
Zoha Irshad Avatar answered Oct 23 '22 01:10

Zoha Irshad