Master date and time in your React admin screens. Learn to use @wordpress/date to format, localize, and manage timestamps in your WordPress plugins.
Previously in this course, we covered Internationalization in React: WordPress Translation for Plugins. While that lesson focused on translating static strings, this lesson shifts our focus to dynamic data: specifically, how to correctly handle and display date and time objects within our Knowledge Base plugin using @wordpress/date.
When building WordPress plugins, you might be tempted to use the native new Date() constructor. Resist this urge. Native JavaScript date objects are notoriously difficult to handle across different timezones, and they don't respect the site-specific settings configured in the WordPress dashboard (Settings > General).
The @wordpress/date package is a wrapper around moment.js (and more recently, dayjs in newer versions) that ensures your plugin behaves exactly like the rest of WordPress. It automatically pulls the site's timezone, date format, and locale settings, providing a consistent experience for both developers and users.
To start, ensure you have the package installed in your project:
npm install @wordpress/date --save
The library provides three primary functions you will use in your admin dashboard:
format: Converts a date string or timestamp into a readable format.date: A wrapper that uses the site's default timezone and format settings.dateI18n: Similar to date, but specifically designed to output localized date strings.In our Knowledge Base project, our REST API returns ISO 8601 strings (e.g., 2023-10-27T10:00:00). Let’s display the "Last Updated" date in our dashboard list component.
JAVASCRIPTimport { format, dateI18n } from CE9178">'@wordpress/date'; const PostDate = ({ dateString }) => { // Format the date using the site's configured settings const formattedDate = dateI18n(CE9178">'F j, Y', dateString); return ( <div className="kb-post-date"> Updated on: {formattedDate} </div> ); };
In the example above, dateI18n takes two arguments: the format string (using PHP date format syntax, not the standard JavaScript Intl.DateTimeFormat) and the date source. Because we are using the familiar Y-m-d or F j, Y syntax, you don't have to learn a new templating language.
In your Knowledge Base dashboard, users often prefer seeing "2 hours ago" rather than an absolute date. Modify your PostDate component to show a relative time string.
moment or use the format utility to calculate the difference.date prop.__unstableFormatRelativeTime utility (or the standard format with a custom logic if you prefer) to display the "time ago" string.Hint: Remember that @wordpress/date functions are designed to keep your plugin UI consistent with the core WordPress experience.
Y-m-d) with JavaScript's Intl formats. WordPress expects the PHP-style tokens.date or dateI18n functions provided by the package instead of native new Date(). Native JS objects often default to the browser's local time, which may differ from the site's configured timezone.Handling time in WordPress isn't just about showing numbers; it's about respecting the user's context. By using @wordpress/date, you ensure that:
As we continue to build out our admin interface, keep in mind that Working with @wordpress/components for WordPress Admin UIs is essential for wrapping these date displays in clean, accessible UI elements like Cards or List rows.
Up next: We will tackle Implementing Drag-and-Drop Sorting, where we'll use these timestamps to order our Knowledge Base entries dynamically.
Learn how to implement drag-and-drop sorting in your WordPress React admin dashboard using @dnd-kit to improve UI/UX for your Knowledge Base plugin.
Read moreLearn how to implement resolvers in @wordpress/data to automate API data fetching. Discover how to sync your React state with the WordPress REST API seamlessly.
Working with Date and Time in React
Building a Modal Confirmation System
Implementing Activity Logging
Using Webpack Aliases
Unit Testing API Endpoints
Unit Testing React Components
Handling Large Datasets with GraphQL
Implementing Real-time Updates with Web