@mui/x-date-pickers utc始终
使用@mui/x-date-pickers
我需要在UTC中显示和编辑。我存储的数据也位于UTC中。我可以将值包装在渲染函数中以以所需的格式强制播放它,但是在编辑后,日期显示在我的语言环境中!我觉得应该可以说:“让我在UTC中展示和编辑。”任何帮助都非常感谢。我简直不敢相信这没有用于科学应用程序。
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
const dateFormat = 'yyyy-MM-dd HH:mm:ss';
const inputMask = '____-__-__ __:__:__';
const views = ['year', 'month', 'day', 'hours', 'minutes', 'seconds'];
const renderUTC = new Date(date).toISOString().replace('T', ' ').substring(0, 19);
...render returning:
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DateTimePicker
value={renderUTC(value)}
...
Using @mui/x-date-pickers
I need to both display and edit in UTC. My stored data is also in UTC. I can wrap the value in a render function to force-display it in the format I need, but then upon edit the date is shown in my locale! I feel like there should be an option to say, "Just let me show and edit in UTC." Any help greatly appreciated. I can't believe this hasn't come up for scientific apps before.
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
const dateFormat = 'yyyy-MM-dd HH:mm:ss';
const inputMask = '____-__-__ __:__:__';
const views = ['year', 'month', 'day', 'hours', 'minutes', 'seconds'];
const renderUTC = new Date(date).toISOString().replace('T', ' ').substring(0, 19);
...render returning:
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DateTimePicker
value={renderUTC(value)}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遇到了同一问题。我的解决方法是使用getTimeZoneOffset()在dateTimePicker中以值和将UTC偏移到本地之后,将其从DateTimePick出来,然后通过OnChange将其偏移到UTC。
这样,DateTimePicker小部件中的DateTime即使在语言环境中也与UTC DateTime相同。
Ran into the same issue. My workaround was to use the getTimezoneOffset() to offset local to UTC before it goes into DateTimePicker as value and offset UTC to local after it comes out of DateTimePicker through onChange.
That way the dateTime in DateTimePicker widget appears same as the UTC dateTime even though it is in locale.
使用 @bikesh-rana的见解是一个完整的解决方案。
这是使用
formik
的&lt; dateTimePicker
。助手功能:并且,在
&lt; dateTimePicker
中:谢谢您的Insight @bikesh-rana!
Using insight from @bikesh-rana here is a complete solution.
This is a
<DateTimePicker
usingFormik
as well. Helper functions:And, in the
<DateTimePicker
:Thank you for the insight @bikesh-rana!