4.2.0 • Published 2 years ago

@woocommerce/date v4.2.0

Weekly downloads
873
License
GPL-3.0-or-later
Repository
github
Last release
2 years ago

Date

A collection of utilities to display and work with date values.

Installation

Install the module

pnpm install @woocommerce/date --save

This package assumes that your code will run in an ES2015+ environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using core-js or @babel/polyfill will add support for these methods. Learn more about it in Babel docs.

Usage

The date package makes use of the global window.wcSettings.timeZone. If a timezone is set, the current and last periods will be converted from your browser's timezone to the store timezone. If none is set, these periods will be based on your browser's timezone.

Functions

Typedefs

appendTimestamp ⇒ string

Adds timestamp to a string date.

Kind: global constant Returns: string - - String date with timestamp attached.

ParamTypeDescription
datemoment.MomentDate as a moment object.
timeOfDaystringEither start, now or end of the day.

getDateValue ⇒ DateValue

Get a DateValue object for a period described by a period, compare value, and start/end dates, for custom dates.

Kind: global constant Returns: DateValue - - DateValue data about the selected period

ParamTypeDescription
periodstringthe chosen period
comparestringprevious_period or previous_year
afterObjectafter date if custom period
beforeObjectbefore date if custom period

getDateParamsFromQueryMemoized ⇒ Object

Memoized internal logic of getDateParamsFromQuery().

Kind: global constant Returns: Object - - date parameters derived from query parameters with added defaults

ParamTypeDescription
periodstringperiod value, ie last_week
comparestringcompare value, ie previous_year
afterstringdate in iso date format, ie 2018-07-03
beforestringdate in iso date format, ie 2018-07-03
defaultDateRangestringthe store's default date range

getDateParamsFromQuery ⇒ DateParams

Add default date-related parameters to a query object

Kind: global constant Returns: DateParams - - date parameters derived from query parameters with added defaults

ParamTypeDescription
queryObjectquery object
query.periodstringperiod value, ie last_week
query.comparestringcompare value, ie previous_year
query.afterstringdate in iso date format, ie 2018-07-03
query.beforestringdate in iso date format, ie 2018-07-03
defaultDateRangestringthe store's default date range

getCurrentDatesMemoized ⇒ Object

Memoized internal logic of getCurrentDates().

Kind: global constant Returns: Object - - Primary and secondary DateValue objects

ParamTypeDescription
periodstringperiod value, ie last_week
comparestringcompare value, ie previous_year
primaryStartObjectprimary query start DateTime, in Moment instance.
primaryEndObjectprimary query start DateTime, in Moment instance.
secondaryStartObjectprimary query start DateTime, in Moment instance.
secondaryEndObjectprimary query start DateTime, in Moment instance.

getCurrentDates ⇒ Object

Get Date Value Objects for a primary and secondary date range

Kind: global constant Returns: Object - - Primary and secondary DateValue objects

ParamTypeDescription
queryObjectquery object
query.periodstringperiod value, ie last_week
query.comparestringcompare value, ie previous_year
query.afterstringdate in iso date format, ie 2018-07-03
query.beforestringdate in iso date format, ie 2018-07-03
defaultDateRangestringthe store's default date range

getDateDifferenceInDays ⇒ number

Calculates the date difference between two dates. Used in calculating a matching date for previous period.

Kind: global constant Returns: number - - Difference in days.

ParamTypeDescription
datestringDate to compare
date2stringSeconary date to compare

getPreviousDate ⇒ Object

Get the previous date for either the previous period of year.

Kind: global constant Returns: Object - - Calculated date

ParamTypeDescription
datestringBase date
date1stringprimary start
date2stringsecondary start
comparestringprevious_period or previous_year
intervalstringinterval

toMoment(format, str) ⇒ Object | null

Convert a string to Moment object

Kind: global function Returns: Object | null - - Moment object representing given string

ParamTypeDescription
formatstringlocalized date string format
strstringdate string

getRangeLabel(after, before) ⇒ string

Given two dates, derive a string representation

Kind: global function Returns: string - - text value for the supplied date range

ParamTypeDescription
afterObjectstart date
beforeObjectend date

getStoreTimeZoneMoment() ⇒ string

Gets the current time in the store time zone if set.

Kind: global function Returns: string - - Datetime string.

getLastPeriod(period, compare) ⇒ DateValue

Get a DateValue object for a period prior to the current period.

Kind: global function Returns: DateValue - - DateValue data about the selected period

ParamTypeDescription
periodstringthe chosen period
comparestringprevious_period or previous_year

getCurrentPeriod(period, compare) ⇒ DateValue

Get a DateValue object for a curent period. The period begins on the first day of the period, and ends on the current day.

Kind: global function Returns: DateValue - - DateValue data about the selected period

ParamTypeDescription
periodstringthe chosen period
comparestringprevious_period or previous_year

getAllowedIntervalsForQuery(query, defaultDateRange) ⇒ Array

Returns the allowed selectable intervals for a specific query.

Kind: global function Returns: Array - Array containing allowed intervals.

ParamTypeDescription
queryObjectCurrent query
defaultDateRangestringthe store's default date range

getIntervalForQuery(query, defaultDateRange) ⇒ string

Returns the current interval to use.

Kind: global function Returns: string - Current interval.

ParamTypeDescription
queryObjectCurrent query
defaultDateRangestringthe store's default date range

getChartTypeForQuery(query) ⇒ string

Returns the current chart type to use.

Kind: global function Returns: string - Current chart type.

ParamTypeDescription
queryObjectCurrent query
query.chartTypestring

getDateFormatsForInterval(interval, ticks, option) ⇒ string

Returns date formats for the current interval.

Kind: global function Returns: string - Current interval.

ParamTypeDescription
intervalstringInterval to get date formats for.
ticksnumberNumber of ticks the axis will have.
optionObjectOptions
option.typestringDate format type, d3 or php, defaults to d3.

getDateFormatsForIntervalD3(interval, ticks) ⇒ string

Returns d3 date formats for the current interval. See https://github.com/d3/d3-time-format for chart formats.

Kind: global function Returns: string - Current interval.

ParamTypeDescription
intervalstringInterval to get date formats for.
ticksnumberNumber of ticks the axis will have.

getDateFormatsForIntervalPhp(interval, ticks) ⇒ string

Returns php date formats for the current interval. See see https://www.php.net/manual/en/datetime.format.php.

Kind: global function Returns: string - Current interval.

ParamTypeDescription
intervalstringInterval to get date formats for.
ticksnumberNumber of ticks the axis will have.

loadLocaleData(config)

Gutenberg's moment instance is loaded with i18n values, which are PHP date formats, ie 'LLL: "F j, Y g:i a"'. Override those with translations of moment style js formats.

Kind: global function

ParamTypeDescription
configObjectLocale config object, from store settings.
config.userLocalestring
config.weekdaysShortArray

validateDateInputForRange(type, value, before, after, format) ⇒ Object

Validate text input supplied for a date range.

Kind: global function Returns: Object - validatedDate - validated date object

ParamTypeDescription
typestringDesignate beginning or end of range, eg before or after.
valuestringUser input value
beforeObject | nullIf already designated, the before date parameter
afterObject | nullIf already designated, the after date parameter
formatstringThe expected date format in a user's locale

DateValue : Object

DateValue Object

Kind: global typedef Properties

NameTypeDescription
labelstringThe translated value of the period.
rangestringThe human readable value of a date range.
aftermoment.MomentStart of the date range.
beforemoment.MomentEnd of the date range.

DateParams : Object

DateParams Object

Kind: global typedef

ParamTypeDescription
aftermoment.Moment | nullIf the period supplied is "custom", this is the after date
beforemoment.Moment | nullIf the period supplied is "custom", this is the before date

Properties

NameTypeDescription
periodstringperiod value, ie last_week
comparestringcompare valuer, ie previous_year

validatedDate : Object

Kind: global typedef Properties

NameTypeDescription
dateObject | nullA resulting Moment date object or null, if invalid
errorstringAn optional error message if date is invalid
4.1.0

2 years ago

4.0.1

2 years ago

4.2.0

2 years ago

3.2.0

2 years ago

4.0.0

2 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-alpha.708

6 years ago