You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Luxon Adapter的MUI Localization Provider无法设置GB日期格式

问题描述

使用搭配Luxon Adapter的MUI Localization Provider,尝试将日期设置为英国(GB)格式dd/mm/yyyy,但始终显示为mm/dd/yyyy格式,无法生效。

日期显示异常截图

以下是简化后的表单组件代码:

import React, { useState, useEffect } from 'react';
import { Grid, TextField, Typography, FormControlLabel, Radio, RadioGroup, FormControl, FormLabel, Button, Autocomplete, Select } from '@mui/material';
import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { Settings, DateTime } from 'luxon';

// Set Luxon to use the UK locale globally
Settings.defaultLocale = 'en-GB';

const FormikMuiTextField = ({ formik, name, label, type = 'text', xs = 12, sm }) => {
    return (
      <Grid item xs={xs} sm={sm}>
        <TextField
          fullWidth
          variant="outlined"
          margin="normal"
          id={name}
          name={name}
          label={label}
          type={type}
          value={formik.values[name]}
          onChange={formik.handleChange}
          onBlur={formik.handleBlur}
          error={formik.touched[name] && Boolean(formik.errors[name])}
          helperText={formik.touched[name] && formik.errors[name]}
          disabled={name === "totalPassengers"} // Disable the field for totalPassengers
          select={type === "select"}
          SelectProps={{ native: true }}
        >
          {type === "select" && ['Option1', 'Option2', 'Option3'].map(option => (
            <option key={option} value={option}>{option}</option>
          ))}
        </TextField>
      </Grid>
    );
  };

  return (
    <LocalizationProvider dateAdapter={AdapterLuxon}>
      <Grid item xs={6}>
        <DatePicker
          label="Departure Date"
          name="departureDate"
          value={formik.values.departureDate ? DateTime.fromISO(formik.values.departureDate) : null}
          onChange={(newValue) => {
            formik.setFieldValue('departureDate', newValue ? newValue.toISO() : '');
          }}
          renderInput={(params) => (
            <TextField
              {...params}
              value={params.inputProps.value ? DateTime.fromISO(params.inputProps.value).toFormat('dd/MM/yyyy') : ''}
            />
          )}
          inputFormat="dd/MM/yyyy"
          fullWidth // Make the DatePicker full width
        />
      </Grid>
    </LocalizationProvider>
  )

package.json依赖:

{
  "name": "umrah-quote-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@date-io/date-fns": "^2.10.0",
    "@date-io/luxon": "^3.0.0",
    "@emotion/react": "^11.11.4",
    "@emotion/styled": "^11.11.5",
    "@mui/lab": "^5.0.0-alpha.170",
    "@mui/material": "^5.15.15",
    "@mui/x-date-pickers": "^7.2.0",
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "cors": "^2.8.5",
    "date-fns": "^2.28.0",
    "express": "^4.19.2",
    "formik": "^2.4.5",
    "luxon": "^3.4.4",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-resizable": "^3.0.5",
    "react-router-dom": "^6.22.3",
    "react-scripts": "5.0.1",
    "react-select": "^5.8.0",
    "web-vitals": "^2.1.4",
    "yup": "^1.4.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

已尝试更换适配器、单独包裹日期字段,均未解决问题。


解决方案

问题核心是自定义renderInput的格式化逻辑与DatePicker内置规则冲突,加上LocalizationProvider未正确绑定英国区域配置,按以下步骤修改:

  1. 移除renderInput中的手动格式化代码
    你手动覆盖了TextField的value,会打断DatePicker基于inputFormat的自动格式化流程,直接使用组件传递的params即可:

    renderInput={(params) => <TextField {...params} fullWidth />}
    
  2. 给LocalizationProvider配置英国locale
    仅设置Luxon全局locale不够,需给AdapterLuxon传入明确的区域参数:

    <LocalizationProvider 
      dateAdapter={AdapterLuxon}
      adapterLocale="en-GB"
    >
      {/* 内部组件 */}
    </LocalizationProvider>
    
  3. 确认inputFormat的正确性
    保持inputFormat="dd/MM/yyyy",注意Luxon规则中MM代表月份(小写mm是分钟),这是格式生效的关键。

  4. 完整修改后的DatePicker代码

    <Grid item xs={6}>
      <DatePicker
        label="Departure Date"
        name="departureDate"
        value={formik.values.departureDate ? DateTime.fromISO(formik.values.departureDate) : null}
        onChange={(newValue) => {
          formik.setFieldValue('departureDate', newValue ? newValue.toISO() : '');
        }}
        inputFormat="dd/MM/yyyy"
        renderInput={(params) => <TextField {...params} fullWidth />}
      />
    </Grid>
    

你的依赖版本是兼容的(@mui/x-date-pickers v7匹配@date-io/luxon v3),无需调整版本。

内容的提问来源于stack exchange,提问作者grimfate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 14:57:08