如何在

发布于 2025-01-19 08:26:51 字数 853 浏览 0 评论 0原文

我想存储一个 DateTimeFormatOptionsdate.toLocaleString() 在我的应用程序的多个位置使用。我将其定义为:

export const timeFormat = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', time  Zone: 'UTC'}

我得到:

Argument of type '{ month: string; day: string; hour: string; minute: string; hour12: boolean; timeZoneName: string; timeZone: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
  Types of property 'month' are incompatible.
    Type 'string' is not assignable to type '"numeric" | "2-digit" | "short" | "long" | "narrow" | undefined'.

但我无法弄清楚 import DateTimeFormatOptions。最终我只是编写了一个格式化日期的辅助方法,但我仍然可能需要导入它,因为我可能允许用户更改日期首选项。

I want to store a DateTimeFormatOptions for date.toLocaleString() to use in multiple places in my app. I defined it like:

export const timeFormat = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', time  Zone: 'UTC'}

And I get:

Argument of type '{ month: string; day: string; hour: string; minute: string; hour12: boolean; timeZoneName: string; timeZone: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
  Types of property 'month' are incompatible.
    Type 'string' is not assignable to type '"numeric" | "2-digit" | "short" | "long" | "narrow" | undefined'.

But I can't figure out import DateTimeFormatOptions. Eventually I just wrote a helper method that formats the date, but I still may need to import it because I may allow the user to change date preferences.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2025-01-26 08:26:51

它位于 Intl对象。您不需要导入它。只需将类型设置为 Intl.DateTimeFormatOptions 即可。

const timeFormat: Intl.DateTimeFormatOptions = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', timeZone: 'UTC' }

It's in the Intl object. You won't need to import it. Just set the type to Intl.DateTimeFormatOptions.

const timeFormat: Intl.DateTimeFormatOptions = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', timeZone: 'UTC' }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文