{"id":134547,"date":"2021-03-09T08:38:23","date_gmt":"2021-03-09T08:38:23","guid":{"rendered":"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/packages\/packages-date\/"},"modified":"2024-12-03T18:04:12","modified_gmt":"2024-12-03T18:04:12","slug":"packages-date","status":"publish","type":"blocks-handbook","link":"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/packages\/packages-date\/","title":{"rendered":"@wordpress\/date"},"content":{"rendered":"
Date module for WordPress.<\/p>\n
Install the module<\/p>\n
npm install @wordpress\/date --save\n<\/code><\/pre>\nThis package assumes that your code will run in an ES2015+<\/strong> environment. If you’re using an environment that has limited or no support for such language features and APIs, you should include the polyfill shipped in @wordpress\/babel-preset-default<\/code><\/a> in your code.<\/em><\/p>\nAPI<\/h2>\n
<\/p>\n
date<\/h3>\n
Formats a date (like date()<\/code> in PHP).<\/p>\nRelated<\/em><\/p>\n\n- https:\/\/en.wikipedia.org\/wiki\/List_of_tz_database_time_zones<\/a><\/li>\n
- https:\/\/en.wikipedia.org\/wiki\/ISO_8601#Time_offsets_from_UTC<\/a><\/li>\n<\/ul>\n
Parameters<\/em><\/p>\n\n- dateFormat<\/em>
string<\/code>: PHP-style formatting string. See php.net\/date.<\/li>\n- dateValue<\/em>
Moment | Date | string | undefined<\/code>: Date object or string, parsable by moment.js.<\/li>\n- timezone<\/em>
string | number | undefined<\/code>: Timezone to output result in or a UTC offset. Defaults to timezone from site.<\/li>\n<\/ul>\nReturns<\/em><\/p>\n\nstring<\/code>: Formatted date in English.<\/li>\n<\/ul>\ndateI18n<\/h3>\n
Formats a date (like wp_date()<\/code> in PHP), translating it into site’s locale.<\/p>\nBackward Compatibility Notice: if timezone<\/code> is set to true<\/code>, the function behaves like gmdateI18n<\/code>.<\/p>\nRelated<\/em><\/p>\n\n- https:\/\/en.wikipedia.org\/wiki\/List_of_tz_database_time_zones<\/a><\/li>\n
- https:\/\/en.wikipedia.org\/wiki\/ISO_8601#Time_offsets_from_UTC<\/a><\/li>\n<\/ul>\n
Parameters<\/em><\/p>\n\n- dateFormat<\/em>
string<\/code>: PHP-style formatting string. See php.net\/date.<\/li>\n- dateValue<\/em>
Moment | Date | string | undefined<\/code>: Date object or string, parsable by moment.js.<\/li>\n- timezone<\/em>
string | number | boolean | undefined=<\/code>: Timezone to output result in or a UTC offset. Defaults to timezone from site. Notice: boolean<\/code> is effectively deprecated, but still supported for backward compatibility reasons.<\/li>\n<\/ul>\nReturns<\/em><\/p>\n\nstring<\/code>: Formatted date.<\/li>\n<\/ul>\nformat<\/h3>\n
Formats a date. Does not alter the date’s timezone.<\/p>\n
Parameters<\/em><\/p>\n\n- dateFormat<\/em>
string<\/code>: PHP-style formatting string. See php.net\/date.<\/li>\n- dateValue<\/em>
Moment | Date | string | undefined<\/code>: Date object or string, parsable by moment.js.<\/li>\n<\/ul>\nReturns<\/em><\/p>\n\nstring<\/code>: Formatted date.<\/li>\n<\/ul>\ngetDate<\/h3>\n
Create and return a JavaScript Date Object from a date string in the WP timezone.<\/p>\n
Parameters<\/em><\/p>\n\n- dateString<\/em>
?string<\/code>: Date formatted in the WP timezone.<\/li>\n<\/ul>\nReturns<\/em><\/p>\n\nDate<\/code>: Date<\/li>\n<\/ul>\ngetSettings<\/h3>\n
Returns the currently defined date settings.<\/p>\n
Returns<\/em><\/p>\n\nDateSettings<\/code>: Settings, including locale data.<\/li>\n<\/ul>\ngmdate<\/h3>\n
Formats a date (like date()<\/code> in PHP), in the UTC timezone.<\/p>\nParameters<\/em><\/p>\n\n- dateFormat<\/em>
string<\/code>: PHP-style formatting string. See php.net\/date.<\/li>\n- dateValue<\/em>
Moment | Date | string | undefined<\/code>: Date object or string, parsable by moment.js.<\/li>\n<\/ul>\nReturns<\/em><\/p>\n\nstring<\/code>: Formatted date in English.<\/li>\n<\/ul>\ngmdateI18n<\/h3>\n
Formats a date (like wp_date()<\/code> in PHP), translating it into site’s locale and using the UTC timezone.<\/p>\nParameters<\/em><\/p>\n\n- dateFormat<\/em>
string<\/code>: PHP-style formatting string. See php.net\/date.<\/li>\n- dateValue<\/em>
Moment | Date | string | undefined<\/code>: Date object or string, parsable by moment.js.<\/li>\n<\/ul>\nReturns<\/em><\/p>\n\nstring<\/code>: Formatted date.<\/li>\n<\/ul>\nhumanTimeDiff<\/h3>\n
Returns a human-readable time difference between two dates, like human_time_diff()<\/a> in PHP.<\/p>\nParameters<\/em><\/p>\n\n- from<\/em>
Moment | Date | string<\/code>: From date, in the WP timezone.<\/li>\n- to<\/em>
Moment | Date | string | undefined<\/code>: To date, formatted in the WP timezone.<\/li>\n<\/ul>\nReturns<\/em><\/p>\n\nstring<\/code>: Human-readable time difference.<\/li>\n<\/ul>\nisInTheFuture<\/h3>\n
Check whether a date is considered in the future according to the WordPress settings.<\/p>\n
Parameters<\/em><\/p>\n\n- dateValue<\/em>
string<\/code>: Date String or Date object in the Defined WP Timezone.<\/li>\n<\/ul>\nReturns<\/em><\/p>\n\nboolean<\/code>: Is in the future.<\/li>\n<\/ul>\nsetSettings<\/h3>\n
Adds a locale to moment, using the format supplied by wp_localize_script()<\/code>.<\/p>\nParameters<\/em><\/p>\n\n- dateSettings<\/em>
DateSettings<\/code>: Settings, including locale data.<\/li>\n<\/ul>\n<\/p>\n
Contributing to this package<\/h2>\n
This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm<\/a> and used by WordPress<\/a> as well as other software projects.<\/p>\n