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!
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.
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.
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)}
/>;
Note: Couldn't find this prop in public facing docs though.
You can use useWeekdaysShort attribute of react-datepicker.
e.g:
<DatePicker>
useWeekdaysShort={true}
</DatePicker>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With