- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序需要 material-ui 日期和时间选择器才能在服务器指定的远程时区上运行。我希望日期选择器上的今天圆圈实际指示远程时区中的今天,并且我想将远程时区中的日期时间转换为自 1970-01-01T00:00:00Z
以来的秒数.
我正在使用material-ui v5 alphas。 docs假设您指定了 @date-io
adapter为您的时间图书馆。看起来有四个明显的选择:
@date-io/date-fns
(基于 date-fns 和 date-fns-tz 有一个损坏的远程时区设计。它使用 Javascript 日期来表示远程时区中的日期和时间,但如果本地时区有“ Spring ”小时,则有你无法代表的时间。issue。@date-io/dayjs
(基于 dayjs )不能正确处理夏令时。 issue @date-io/luxon
(基于 luxon )看起来很有希望 @date-io/moment
(基于 moment 和 moment-timezone )看起来很有希望 You can pass moment-time zone right to the
libInstance
in this case it will use time zone set of the moment instance or global one
@date-io/luxon
构造函数似乎不允许像今天这样覆盖实例。
$ mkdir tztest
$ cd tztest
$ npm init -y
$ npm install --save moment moment-timezone '@date-io/moment'
$ node
> let MomentUtils = require('@date-io/moment');
undefined
> let moment = require('moment');
undefined
> let _ = require('moment-timezone');
undefined
> // Operations including the following should all work similarly to when using the default instance:
> (new MomentUtils()).date();
Moment<2021-03-18T11:57:30-07:00>
> (new MomentUtils()).date('2021-01-01T00:00:00');
Moment<2021-01-01T00:00:00-08:00>
> (new MomentUtils()).getCurrentLocaleCode();
'en'
> // Here's some garbage I tried
> (new MomentUtils({instance: moment().tz('America/New_York')})).date();
Uncaught TypeError: _this.moment is not a function
at MomentUtils.date (/Users/slamb/git/tztest/node_modules/@date-io/moment/build/index.js:78:32)
> (new MomentUtils({instance: moment.tz('America/New_York')})).date();
Uncaught TypeError: _this.moment is not a function
at MomentUtils.date (/Users/slamb/git/tztest/node_modules/@date-io/moment/build/index.js:78:32)
> (new MomentUtils({instance: () => moment.tz('America/New_York')})).date();
Moment<2021-03-18T14:44:07-04:00>
> (new MomentUtils({instance: () => moment.tz('America/New_York')})).date('2021-01-01T00:00:00');
Moment<2021-03-18T14:44:19-04:00>
> (new MomentUtils({instance: (arg1, arg2, arg3, arg4) => moment.tz(arg1, arg2, arg3, arg4, 'America/New_York')})).date('2021-01-01T00:00:00');
Moment<2021-01-01T00:00:00-05:00>
> (new MomentUtils({instance: (arg1, arg2, arg3, arg4) => moment.tz(arg1, arg2, arg3, arg4, 'America/New_York')})).getCurrentLocaleCode();
Uncaught TypeError: _this.moment.locale is not a function
at MomentUtils.getCurrentLocaleCode (/private/tmp/tztest/node_modules/@date-io/moment/build/index.js:63:49)
> (new MomentUtils({instance: (arg1, arg2, arg3, arg4) => moment.tz(arg1, arg2, arg3, arg4, 'America/New_York')})).date();
Moment<2021-03-18T14:44:36-04:00>
> (new MomentUtils({instance: function() { return moment(arguments).tz('America/New_York'); } })).date()
...here the interpreter started making fun of me...
来自
@date-io/moment
source ,如下所述,我看到它以几种不同的方式使用它。当然,我希望所有这些都能正常工作。
export default class MomentUtils implements IUtils<defaultMoment.Moment> {
...
constructor({ locale, formats, instance }: Opts = {}) {
this.moment = instance || defaultMoment;
...
return /A|a/.test(this.moment().localeData().longDateFormat("LT"));
...
return this.moment.localeData().longDateFormat(token as LongDateFormatKey);
...
return this.locale || this.moment.locale();
...
return this.moment(value, format, this.locale, true);
...
return this.moment(value, format, true);
...
const moment = this.moment(value);
...
return this.moment.weekdaysShort(true);
最佳答案
问题是 material-ui 针对不同的主要版本有不同的文档和日期适配器集成 api。
我得到了moment-timezone
与 @material-ui/pickers@4.0.0-alpha.11
合作
依赖关系
"@material-ui/core": "4.11.3",
"@material-ui/pickers": "4.0.0-alpha.11",
"moment": "2.29.1",
"moment-timezone": "0.5.33",
"react": "17.0.2",
"react-dom": "17.0.2"
工作演示
import React from "react";
import { render } from "react-dom";
import momentTimezone from "moment-timezone";
import { TextField } from "@material-ui/core";
import { DatePicker, LocalizationProvider } from "@material-ui/pickers";
import MomentAdapter from "@material-ui/pickers/adapter/moment";
const App = () => {
const timeZoneFromServer = "Asia/Tokyo";
const { moment } = new MomentAdapter({ instance: momentTimezone });
const dateWithTimeZone = moment().tz(timeZoneFromServer);
const handleDateChange = () => {};
return (
<LocalizationProvider dateAdapter={MomentAdapter}>
<DatePicker
renderInput={(props) => <TextField {...props} />}
value={dateWithTimeZone}
onChange={handleDateChange}
/>
</LocalizationProvider>
);
};
render(<App />, document.getElementById("root"));
关于javascript - 远程时区的material-ui LocalizationProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66697563/
在我的 Rails (3.0) 应用程序中,我使用助手 time_zone_select() 来使用时区。它生成时区名称,如“(GMT+01:00) Paris”... 但这些名称与 MySQL 中的
我正在解析格式为的日期: SimpleDateFormat formatter = new SimpleDateFormat("EE MMM dd HH:mm:ss Z yyyy"); 当我这样做的时
我在 Android 上使用 ACTION_TIMEZONE_CHANGED Intent 过滤器来响应时区更改。 我注意到 Jodas 当前时区此时没有更新,使用:DateTimeZone.getD
我有一个旧版 Windows 应用程序,它从数据库读取数据。其中一列是“TimeZoneInfoId”。在传统世界中,它是由另一个 Windows 应用程序编写的,因此它存储 Windows 字符串:
我是数据库开发新手,目前从事 MySQL 工作。我有一列包含美国的所有时区。我希望能够获取每行中每个时区的当前时间(只有时间,没有日期)。 时区数据如下:+05:00、-03:00 等等 这就是我尝试
我正在尝试了解 session 时区在 PostgreSQL 中的工作原理。 当我将 session 时区设置为“01:00”时,返回日期为“-01:00”。符号总是颠倒的,我不理解这种行为。 这怎么
我写入我的 mongoDB 数据库,例如开始日期,开始日期始终是一周中的星期一(我使用并且我在欧洲中部时区)。 "startDate" : ISODate("2014-12-28T23:00:00Z"
是否有详尽的 MySQL 时区列表? 似乎 MySQL 设置中 time_zone 的有效值取决于主机操作系统,但我一直无法找到可能值的列表。 我需要时间显示卡尔加里本地时间。 最佳答案 默认情况下,
我已经在 config/app.php 中配置了时区至 Europe/Lisbon . 如果我做 return date_default_timezone_get(); , 返回 Europe/Lis
我在整个脚本中广泛使用 unix 时间,通常我将时区设置如下: date_default_timezone_set('Europe/London'); 这一直工作正常,直到上周末时钟回退一个小时,我怀
我有一个字符串,2013-10-07T23:59:51.205-07:00,想将其转换为 Java 日期对象。我收到解析错误。 date = new SimpleDateFormat("yyyy-MM
如何更改日志文件名中的 log4j 时区? 我的 log4j.xml 文件:
在JAVA中,如何确定所有日期都作为GMT日期返回? 例如,即使我尝试使用GMT语言环境强制使用DateFormat,它也会应用某种逻辑来检索BST日期。 public static void mai
好吧,这是我遇到过的最奇怪的错误之一! 首先:我不是 Python 程序员,该脚本是由 friend 编写的(我认为他的大部分内容来自示例)。 该脚本的用途:将日历信息(CET 时间)从 XML 文件
我们有一个网站,目前在某个时间(由用户选择)运行拍卖,这一切都工作正常,因为当服务器到达该时间时,拍卖就会开始。然后我们必须添加时区,具体取决于用户居住的地方。然后,根据所选的下拉菜单,这将增加或减少
我陷入了这个逻辑。我正在开发一个消息调度项目,我的客户来自不同的国家。让我们考虑一下亚洲(不遵守夏令时)和美国/纽约(遵守夏令时)。 现在,我正在编写以下查询来获取距当前时间 10 分钟间隔内的时间表
我尝试返回特定时区的值,但我遇到了一些奇怪的响应行为: SELECT created_at AT TIME ZONE 'US/Pacific' - created_at, NOW() A
我正在尝试将不带时区值(例如“31.05.2015”)的字符串转换为 NSDate。 我的 DateFormatter 有什么问题?它返回日期 2015-05-31 22:00:00 +0000当我尝
我已经成功地将我的本地 tzinfo 导入到 mysql 中,我几乎已经了解了关于如何存储和使用这些数据的所有细节,除了一个字段。有一个名为 Transition_type_id 的数字字段(在表 t
为什么以下行返回的 TimeZone 显示不正确的时间: TimeZone.getTimeZone("America/Ottawa") 现在显示晚上 10:26 [亚特兰大时间现在是 6:26,Ott
我是一名优秀的程序员,十分优秀!