11

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

Damon
  • 4,151
  • 13
  • 52
  • 108

6 Answers6

27

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.

Manish
  • 4,903
  • 1
  • 29
  • 39
5

You can use useWeekdaysShort attribute of react-datepicker.

e.g:

<DatePicker>
useWeekdaysShort={true}
</DatePicker>
Zoha Irshad
  • 427
  • 5
  • 5
3

Use:

dateFormat={"EEEE"}

Or:

dateFormat={"EEE"}

NK99Dev
  • 61
  • 7
0

You can use a prop called dateFormat to format the date to your desired output. To get the days as Sun Mon Tue, pass dateFormat="ddd" as a props to the component. Like this way

<DatePicker
dateFormat="ddd"
onChange={this.handleChange} />
  • 2
    Thank you for your suggestion. I've updated my question to show how I'm passing in the properties. I've tried adding `dateFormat` but no luck. Maybe I'm not passing it correctly? – Damon Jun 15 '19 at 15:30
0

The DayPicker allows you to pass custom elements for the days of the week using the weekdayElement prop. Thus, you could create a Weekday element, translate the name to the long name using the formatWeekdayLong method from react-day-picker utils, and cut that name down to a string of 3 characters. Something like the following worked for me:

const Weekday = ({ weekday, className, localeUtils, locale }) => {
    const longWeekday = localeUtils.formatWeekdayLong(weekday, locale);
    const newWeekday = longWeekday.substring(0,3);

    return <div className={className}>{newWeekday}</div>;
};


// Date Picker Component
const InputDatePicker = () => {
  return (
    <div className='input-date-picker__container'>
      <DayPicker
        weekdayElement={<Weekday />}
      />
    </div>
  );
};

If you look at the localeUtils parameter passed to the Weekday component you will see there are a lot of handy methods passed there.

binkie
  • 865
  • 6
  • 9
0

As said, you can use the prop formatWeekDay. Pass a function that should be used to format the week day and everything will work correctly!

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