What is the method for setting the start day of the week to Wednesday in material-ui-pickers?

Is there a way to set Wednesday as the start day of the week in material-ui-pickers?

By default, Sunday is being displayed as the start day. Is there a method to change this setting?

Appreciate any help on this matter.

Answer №1

Time.js

time.locale('en', { weekday: { startOf: 3 } }) // 0-6 (Sunday-Saturday)

startOf: starting day of the week (https://github.com/time/timelywebsite/issues/279)

Complete example:

import React, { ReactElement, useEffect, useState } from 'react'
import { TimePicker, MuiPickersUtilsProvider } from '@material-ui/pickers'
import MomentUtils from '@date-io/moment'
import time from 'time'
import 'time/locale/de'

export const TimePickerComponent = (): ReactElement => {
    const [selectedDate, handleDateChange] = useState(new Date())

    useEffect(() => {
        time.locale('de', { weekday: { startOf: 3 } })
    }, [])

    return (
        <MuiPickersUtilsProvider utils={MomentUtils}>
            <TimePicker value={selectedDate} onChange={handleDateChange} />
        </MuiPickersUtilsProvider>
    )
}


day-fns

local.options.weekBeginsOn = 3 // 0-6 (Sunday-Saturday)
<MuiPickersUtilsProvider utils={DayFnsUtils} locale={locale}>

Complete example:

import React, { ReactElement, useState } from 'react'
import { TimePicker, MuiPickersUtilsProvider } from '@material-ui/pickers'
import DayFnsUtils from '@date-io/day-fns'
import locale from 'day-fns/locale/en-US'

if (locale && locale.options) {
    locale.options.weekBeginsOn = 3
}

export const DayFNSPicker = (): ReactElement => {
    const [selectedDate, handleDateChange] = useState(new Date())

    return (
        <MuiPickersUtilsProvider utils={DayFnsUtils} locale={locale}>
            <TimePicker value={selectedDate} onChange={handleDateChange} />
        </MuiPickersUtilsProvider>
    )
}

Answer №2

To ensure the correct date format, I had to specify British locale in LocalizationProvider.

import enLocale from 'date-fns/locale/en-GB';

...
  return (
      <LocalizationProvider locale={enLocale} ..>
        <CalendarPicker ... />
      </LocalizationProvider>
  );

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to customize the style of a component buried deep within a hierarchy? (Utilizing Material-UI's JSS Sty

The DOM structure for Material UI TextField is as follows: <FormControl... > <BaseInput ...> <input class ="MuiInputBase-input-29" ...> </BaseInput> </FormControl> When I add anything to the TextField's ...

Exploring the integration of Jest/Enzyme for testing React components while maintaining JSS style encapsulation

I've been facing some challenges while testing my React components with Jest due to the encapsulation of JSS components. Here's a pseudo code example: JSS(style.js): export default { pinkOnYellow: { color: 'pink', b ...

What is the best way to retrieve the name and ID values from a select

Using react hooks, I have created a select component. Currently, I am able to retrieve the id of the selected country but not the name. Here is the code for the select component: const [countries, setCountries] = useState([]); const [selectedCountry, set ...

Error: Unable to locate module: Material-UI - Please check the path and try again

Encountering this error: Error message: Failed to compile ./node_modules/@material-ui/core/Modal/Modal.js Module not found: Can't resolve '@babel/runtime/helpers/builtin/assertThisInitialized' in 'C:\Users\rifat\ ...

State update failing to modify arrays

Shown below is an array that contains boolean values: const [state, setState] = React.useState({ [`${"checkedA"+index}`]: false, [`${"checkedB"+index}`]: false, [`${"checkedC"+index}`]: false, [`${"checkedD"+index}`]: false, }); ...

What is the best way to access the variant value within the content of SnackbarProvider component

Currently, I am utilizing the notistack library to display snackbars in my application. To personalize the content of the snackbar, I am using the content property of the snackbar. My goal is to determine whether the message variant is success, warning, or ...

The issue of Material UI breaking after a hard refresh in Next.js

Encountering an issue with a fresh nextjs application where Material UI has been configured Every time I perform a hard refresh on the page, the styling gets messed up and requires me to make modifications in each file to restore the previous style... Sh ...

What is the best way to position the label above the input text in a Material UI TextField component with a Start Adornment?

Struggling to figure out the right Material UI CSS class to style a reusable TextField component? You're not alone. Despite tinkering with InputLabelProps (specifically the shrink class), I can't seem to get it right. Here's the code snippet ...

Challenges with implementing singleSelect feature in MUI-X DataGrid

Encountering an issue with the singleSelect type on the community version of x-data-grid. The problem arises when attempting to edit a row, where my singleSelect consists of the following data set. Here is how I have configured my DataGrid setup. Although ...

Setting Default Value for Autocomplete in React

I'm facing an issue with the default value in my autocomplete feature within my React app. Even though I set a default value, when I try to submit the form, it still shows as invalid because it appears to have no value until I manually click on the au ...

Create a custom file uploading feature in React with Material-UI

I am currently working on developing a compact file upload component. Previously, I utilized the <input type="file"> tag but due to limited space constraints, I need to display both "Choose file" and the complete file path within a very sma ...

What is the best way to vertically align a Material UI component to occupy the remaining space within a container

Issue with Material UI/Grids for Login Page As a newcomer to Material UI/Grids, I am currently working on creating a login page where the layout is split into two parts. The left side occupies 5 column spaces while the right side takes up 7 column spaces. ...

Float over Material UI Icon and Text

Currently, I am tackling a React JS Project. My task involves creating a breadcrumb that contains both text and an icon. To accomplish this, I have incorporated a material UI icon. import UpdateIcon from "@material-ui/icons/Update"; ...

The React MaterialUi Date Picker is causing a RangeError due to an invalid time value

I've exhausted all available resources in search of a solution for this persistent error, but unfortunately, I have not been successful. Most suggestions revolve around format="DD/MM/YYYY HH:mm" or utilizing Moment (depending on locale) Al ...

How can the vertical scroll bar position be reset in Material-Table when pagination is activated?

Whenever I scroll up or down in my Material-Table, and then switch pages, the vertical scroll bar doesn't reset to the top of the table. Instead, it stays where it was before changing the page, causing confusion for users. The only mention of scroll ...

"Facing issues with the menu not showing up when right-clicking on react-big-calendar

I am facing an issue with displaying a menu on right-click using react-big-calendar and material UI. The problem is that the menu is not appearing correctly in the HTML, it's showing up in the top right corner. Below is my code: const handleClick = ...

Even after installing version 6 of MUI X data grid, the APIRef methods cannot be located

Currently, I am in the process of developing data tables for an application using Material UI's data grids. The community version has been sufficient for my needs so far, and I have not felt the need to upgrade to the pro or premium versions yet. Upo ...

A guide to building a versatile dropdown component with Material UI or MUI in a React application

Is there a way to create a reusable dropdown component using Material UI or MUI in React? ...

React's hover effect is running sluggishly due to delays in state updates for hover effects

When I attempt to change the background color of a list item upon user hover, I notice that as the list size increases, the hover effect slows down significantly, giving the impression of lag on the site. I am utilizing the Tree View component from materi ...

Personalizing input field in Material UI auto-suggestion feature

Currently, I am utilizing Material UI Autocomplete to showcase a list of partners. The options are displayed in the format depicted in the image linked here: https://i.stack.imgur.com/3K47G.png The arrangement of the options is as follows: [name] [type] ...