yes*_*nes 32 datetime typescript
我想存储一个DateTimeFormatOptionsfordate.toLocaleString()以便在我的应用程序中的多个位置使用。我把它定义为:
export const timeFormat = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', time Zone: 'UTC'}
Run Code Online (Sandbox Code Playgroud)
我得到:
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'.
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚 import DateTimeFormatOptions。最终我只是编写了一个格式化日期的辅助方法,但我仍然可能需要导入它,因为我可能允许用户更改日期首选项。
Dom*_*ang 68
它在Intl对象中。您不需要导入它。只需将类型设置为Intl.DateTimeFormatOptions.
const timeFormat: Intl.DateTimeFormatOptions = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', timeZone: 'UTC' }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13672 次 |
| 最近记录: |