| .. | ||
| CHANGELOG.md | ||
| PREVIOUS_CHANGELOG.md | ||
| README.md | ||
日期
一组用于显示和处理日期值的实用工具。
安装
安装模块
pnpm install @woocommerce/date --save
此包假定您的代码将在 ES2015+ 环境中运行。如果您使用的环境对 ES2015+ 支持有限或完全不支持(例如较低版本的 IE),那么使用 core-js 或 @babel/polyfill 将为这些方法添加支持。在 Babel 文档 中了解更多信息。
用法
date 包使用全局的 window.wcSettings.timeZone。如果设置了时区,当前和最后周期将从您的浏览器时区转换为商店时区。如果未设置,这些周期将基于您的浏览器时区。
函数
- appendTimestamp ⇒
string 向字符串日期添加时间戳。
- getDateValue ⇒
DateValue 获取一个由周期、比较值以及自定义日期的开始/结束日期所描述的日期范围的 DateValue 对象。
- getDateParamsFromQueryMemoized ⇒
Object getDateParamsFromQuery() 的已记忆化内部逻辑。
- getDateParamsFromQuery ⇒
DateParams 向查询对象添加默认的日期相关参数
- getCurrentDatesMemoized ⇒
Object getCurrentDates() 的已记忆化内部逻辑。
- getCurrentDates ⇒
Object 获取主要和次要日期范围的日期值对象
- getDateDifferenceInDays ⇒
number 计算两个日期之间的天数差。用于计算上一周期的匹配日期。
- getPreviousDate ⇒
Object 获取上一周期或上一年的日期。
- toMoment(format, str) ⇒
Object|null 将字符串转换为 Moment 对象
- getRangeLabel(after, before) ⇒
string 给定两个日期,推导出字符串表示形式
- getStoreTimeZoneMoment() ⇒
string 如果已设置,获取店铺时区的当前时间。
- getLastPeriod(period, compare) ⇒
DateValue 获取当前周期之前一个周期的 DateValue 对象。
- getCurrentPeriod(period, compare) ⇒
DateValue 获取当前周期的 DateValue 对象。该周期从该周期的第一天开始,到当前天结束。
- getAllowedIntervalsForQuery(query, defaultDateRange) ⇒
Array 返回特定查询允许的可选择间隔。
- getIntervalForQuery(query, defaultDateRange) ⇒
string 返回要使用的当前间隔。
- getChartTypeForQuery(query) ⇒
string 返回要使用的当前图表类型。
- getDateFormatsForInterval(interval, [ticks], [option]) ⇒
string 返回当前间隔的日期格式。
- getDateFormatsForIntervalD3(interval, [ticks]) ⇒
string 返回当前间隔的 d3 日期格式。 图表格式请参见 https://github.com/d3/d3-time-format。
- getDateFormatsForIntervalPhp(interval, [ticks]) ⇒
string 返回当前间隔的 php 日期格式。 请参见 https://www.php.net/manual/en/datetime.format.php。
- loadLocaleData(config)
古腾堡的 moment 实例加载了 i18n 值,这些值是 PHP 日期格式,例如 'LLL: "F j, Y g:i a"'。用 moment 风格 js 格式的翻译覆盖它们。
- validateDateInputForRange(type, value, [before], [after], format) ⇒
Object 验证为日期范围提供的文本输入。
类型定义
- DateValue :
Object DateValue 对象
- DateParams :
Object DateParams 对象
- validatedDate :
Object
appendTimestamp ⇒ string
向字符串日期添加时间戳。
Kind: 全局常量
Returns: string - - 附加了时间戳的字符串日期。
| Param | Type | 描述 |
|---|---|---|
| date | moment.Moment |
作为 moment 对象的日期。 |
| timeOfDay | string |
一天的 开始、现在 或 结束。 |
getDateValue ⇒ DateValue
获取一个 DateValue 对象,用于描述由时间段、比较值以及开始/结束日期定义的自定义日期范围。
种类:全局常量
返回:DateValue - - 关于所选时间段的 DateValue 数据
| 参数 | 类型 | 描述 |
|---|---|---|
| period | string |
所选时间段 |
| compare | string |
previous_period 或 previous_year |
| [after] | Object |
自定义时间段的后日期 |
| [before] | Object |
自定义时间段的前日期 |
getDateParamsFromQueryMemoized ⇒ Object
getDateParamsFromQuery() 的已记忆化内部逻辑。
种类:全局常量
返回:Object - - 从查询参数派生的日期参数,并添加了默认值
| 参数 | 类型 | 描述 |
|---|---|---|
| period | string |
期间值,例如 last_week |
| compare | string |
比较值,例如 previous_year |
| after | string |
iso 日期格式的日期,例如 2018-07-03 |
| before | string |
iso 日期格式的日期,例如 2018-07-03 |
| defaultDateRange | string |
商店的默认日期范围 |
getDateParamsFromQuery ⇒ DateParams
向查询对象添加默认的日期相关参数
种类: 全局常量
返回: DateParams - - 从查询参数派生并添加了默认值的日期参数
| 参数 | 类型 | 描述 |
|---|---|---|
| query | Object |
查询对象 |
| query.period | string |
期间值,例如 last_week |
| query.compare | string |
比较值,例如 previous_year |
| query.after | string |
iso 日期格式的日期,例如 2018-07-03 |
| query.before | string |
iso 日期格式的日期,例如 2018-07-03 |
| defaultDateRange | string |
商店的默认日期范围 |
getCurrentDatesMemoized ⇒ Object
getCurrentDates() 的已记忆化内部逻辑。
种类:全局常量
返回:Object - - 主要和次要的 DateValue 对象
| 参数 | 类型 | 描述 |
|---|---|---|
| period | string |
期间值,例如 last_week |
| compare | string |
比较值,例如 previous_year |
| primaryStart | Object |
主要查询起始日期时间,为 Moment 实例。 |
| primaryEnd | Object |
主要查询起始日期时间,为 Moment 实例。 |
| secondaryStart | Object |
主要查询起始日期时间,为 Moment 实例。 |
| secondaryEnd | Object |
主要查询起始日期时间,为 Moment 实例。 |
getCurrentDates ⇒ Object
获取主要和次要日期范围的日期价值对象
种类:全局常量
返回:Object - - 主要和次要的 DateValue 对象
| 参数 | 类型 | 描述 |
|---|---|---|
| query | Object |
查询对象 |
| query.period | string |
期间值,例如 last_week |
| query.compare | string |
比较值,例如 previous_year |
| query.after | string |
ISO 日期格式的日期,例如 2018-07-03 |
| query.before | string |
ISO 日期格式的日期,例如 2018-07-03 |
| defaultDateRange | string |
商店的默认日期范围 |
getDateDifferenceInDays ⇒ number
计算两个日期之间的日期差。用于计算上一周期的匹配日期。
种类:全局常量
返回:number - 天数差。
| 参数 | 类型 | 描述 |
|---|---|---|
| date | string |
要比较的日期 |
| date2 | string |
次要的比较日期 |
getPreviousDate ⇒ Object
获取上一周期或上一年的上一日期。
种类:全局常量
返回:Object - - 计算出的日期
| 参数 | 类型 | 描述 |
|---|---|---|
| date | string |
基准日期 |
| date1 | string |
主要开始日期 |
| date2 | string |
次要开始日期 |
| compare | string |
previous_period 或 previous_year |
| interval | string |
间隔 |
toMoment(format, str) ⇒ Object | null
将字符串转换为 Moment 对象
种类:全局函数
返回:Object | null - 表示给定字符串的 Moment 对象
| 参数 | 类型 | 描述 |
|---|---|---|
| format | string |
本地化日期字符串格式 |
| str | string |
日期字符串 |
getRangeLabel(after, before) ⇒ string
给定两个日期,推导出字符串表示形式
种类: 全局函数
返回: string - - 所提供日期范围的文本价值
| 参数 | 类型 | 描述 |
|---|---|---|
| after | Object |
起始日期 |
| before | Object |
中止日期 |
getStoreTimeZoneMoment() ⇒ string
获取商店时区中的当前时间(如果已设置)。
Kind: 全局函数
Returns: string - - 日期时间字符串。
getLastPeriod(period, compare) ⇒ DateValue
获取当前周期之前一个周期的 DateValue 对象。
种类:全局函数
返回:DateValue - - 关于所选周期的 DateValue 数据
| 参数 | 类型 | 描述 |
|---|---|---|
| period | 字符串 |
选择的周期 |
| compare | 字符串 |
previous_period 或 previous_year |
getCurrentPeriod(period, compare) ⇒ DateValue
获取当前周期的 DateValue 对象。该周期从周期的第一天开始,到当前天结束。
类型: 全局函数
返回: DateValue - - 关于所选周期的 DateValue 数据
| 参数 | 类型 | 描述 |
|---|---|---|
| period | string |
所选周期 |
| compare | string |
previous_period 或 previous_year |
getAllowedIntervalsForQuery(query, defaultDateRange) ⇒ Array
返回特定查询允许的可选择间隔。
种类: 全局函数
返回: Array - 包含允许间隔的数组。
| 参数 | 类型 | 描述 |
|---|---|---|
| query | Object |
当前查询 |
| defaultDateRange | string |
商店的默认日期范围 |
getIntervalForQuery(query, defaultDateRange) ⇒ string
返回要使用的当前间隔。
种类:全局函数
返回:string - 当前间隔。
| 参数 | 类型 | 描述 |
|---|---|---|
| query | Object |
当前查询 |
| defaultDateRange | string |
商店的默认日期范围 |
getChartTypeForQuery(query) ⇒ string
返回当前要使用的图表类型。
Kind: 全局函数
Returns: string - 当前图表类型。
| 参数 | 类型 | 描述 |
|---|---|---|
| query | Object |
当前查询 |
| query.chartType | string |
getDateFormatsForInterval(interval, [ticks], [option]) ⇒ string
返回当前间隔的日期格式。
Kind: 全局函数
Returns: string - 当前间隔。
| 参数 | 类型 | 描述 |
|---|---|---|
| interval | string |
要获取日期格式的间隔。 |
| [ticks] | number |
坐标轴将具有的刻度数量。 |
| [option] | Object |
选项 |
| [option.type] | string |
日期格式类型,d3 或 php,默认为 d3。 |
getDateFormatsForIntervalD3(interval, [ticks]) ⇒ string
返回当前间隔的 d3 日期格式。 图表格式请参见 https://github.com/d3/d3-time-format。
类型: 全局函数
返回: string - 当前间隔。
| 参数 | 类型 | 描述 |
|---|---|---|
| interval | string |
要获取日期格式的间隔。 |
| [ticks] | number |
坐标轴将具有的刻度数量。 |
getDateFormatsForIntervalPhp(interval, [ticks]) ⇒ string
返回当前间隔的 PHP 日期格式。 参见 https://www.php.net/manual/en/datetime.format.php。
Kind: 全局函数
Returns: string - 当前间隔。
| 参数 | 类型 | 描述 |
|---|---|---|
| interval | string |
要获取日期格式的间隔。 |
| [ticks] | number |
坐标轴将具有的刻度数。 |
loadLocaleData(config)
古腾堡的 moment 实例加载了 i18n 值,这些值是 PHP 日期格式,例如 'LLL: "F j, Y g:i a"'。使用 moment 风格 js 格式的翻译来覆盖这些值。
类型:全局函数
| 参数 | 类型 | 描述 |
|---|---|---|
| config | Object |
区域设置配置对象,来自商店设置。 |
| config.userLocale | string |
|
| config.weekdaysShort | Array |
validateDateInputForRange(type, value, [before], [after], format) ⇒ Object
验证为日期范围提供的文本输入。
种类:全局函数
返回:Object - validatedDate - 已验证的日期对象
| 参数 | 类型 | 描述 |
|---|---|---|
| type | string |
指定范围的开始或结束,例如 before 或 after。 |
| value | string |
用户输入值 |
| [before] | Object | null |
如果已指定,则为 before 日期参数 |
| [after] | Object | null |
如果已指定,则为 after 日期参数 |
| format | string |
用户区域设置中预期的日期格式 |
DateValue : Object
日期值对象
类型: 全局类型定义 属性
| 名称 | 类型 | 描述 |
|---|---|---|
| label | string |
周期的翻译值。 |
| range | string |
日期范围的人类可读值。 |
| after | moment.Moment |
日期范围的开始。 |
| before | moment.Moment |
日期范围的结束。 |
DateParams : Object
DateParams 对象
Kind: 全局类型定义
| 参数 | 类型 | 描述 |
|---|---|---|
| after | moment.Moment | null |
如果提供的周期为"custom",则此为 after 日期 |
| before | moment.Moment | null |
如果提供的周期为"custom",则此为 before 日期 |
属性
| 名称 | 类型 | 描述 |
|---|---|---|
| period | string |
周期值,例如 last_week |
| compare | string |
比较值,例如 previous_year |
validatedDate : Object
类型:全局类型定义 属性
| 名称 | 类型 | 描述 |
|---|---|---|
| date | Object | null |
一个结果 Moment 日期对象,如果无效则为 null |
| error | string |
如果日期无效,可选的错误消息 |