Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 34 of the Intermediate WordPress Plugins: REST API & React Admin course
WordPressJune 26, 20263 min read

Working with Date and Time in React: @wordpress/date Tutorial

Master date and time in your React admin screens. Learn to use @wordpress/date to format, localize, and manage timestamps in your WordPress plugins.

WordPressReactJavaScript@wordpress/datePlugin DevelopmentREST APIphpplugin-development

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.

Why avoid native JavaScript 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.

The @wordpress/date API

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:

  1. format: Converts a date string or timestamp into a readable format.
  2. date: A wrapper that uses the site's default timezone and format settings.
  3. dateI18n: Similar to date, but specifically designed to output localized date strings.

Formatting Knowledge Base Timestamps

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.

JAVASCRIPT
import { 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.

Hands-on Exercise: Displaying Relative Time

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.

  1. Import moment or use the format utility to calculate the difference.
  2. Update your component to accept a date prop.
  3. Use the __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.

Common Pitfalls

  • Mixing Formats: Don't confuse PHP date formats (like Y-m-d) with JavaScript's Intl formats. WordPress expects the PHP-style tokens.
  • Timezone Mismatch: Always use the 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.
  • Missing Dependencies: If you find that your dates aren't updating when the site settings change, ensure you are not caching the date string in a local component state unnecessarily.

Recap

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:

  • Dates match the site's admin settings.
  • Timezones are handled server-side to client-side correctly.
  • Translations for month names and day labels are applied automatically.

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.

Previous lessonOptimizing API Response TimesNext lesson Implementing Drag-and-Drop Sorting
Back to Blog

Similar Posts

WordPressJune 26, 20263 min read

Implementing Drag-and-Drop Sorting in WordPress React Plugins

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 more
WordPressJune 25, 20263 min read

Implementing Resolvers for Data Fetching in WordPress @wordpress/data

Learn 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.

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 34 of 45

  1. 1

    Setting up the WordPress Development Environment

    3 min
  2. 2

    Introduction to @wordpress/scripts

    3 min
  3. 3

    Configuring ESLint and Prettier

    3 min
Read more
WordPressJune 25, 20263 min read

Scaffolding the React Admin Dashboard for WordPress Plugins

Learn how to scaffold your React admin dashboard by registering a WordPress menu, creating a root container, and mounting your application into the DOM.

Read more
4

Localizing Data for JavaScript

3 min
  • 5

    Anatomy of a REST API Endpoint

    3 min
  • 6

    Implementing REST API Permission Callbacks

    3 min
  • 7

    Handling GET Requests in REST API

    3 min
  • 8

    Validating and Sanitizing API Arguments

    4 min
  • 9

    Creating POST Endpoints for Data Submission

    3 min
  • 10

    Updating Existing API Resources

    3 min
  • 11

    Handling Asynchronous State in React

    3 min
  • 12

    Building the Knowledge Base Service Layer

    3 min
  • 13

    Scaffolding the React Admin Dashboard

    3 min
  • 14

    Working with @wordpress/components

    3 min
  • 15

    Creating a React Form for Submissions

    3 min
  • 16

    Implementing CRUD in the Admin UI

    3 min
  • 17

    Understanding WordPress Data Store Architecture

    4 min
  • 18

    Registering a Custom Data Store

    3 min
  • 19

    Writing Selectors for Data Access

    3 min
  • 20

    Defining Actions and Reducers

    3 min
  • 21

    Implementing Resolvers for Data Fetching

    3 min
  • 22

    Optimizing Performance with Selectors

    3 min
  • 23

    Handling Complex State Dependencies

    4 min
  • 24

    Implementing Nonce Verification

    4 min
  • 25

    Advanced Sanitization Techniques

    3 min
  • 26

    Input Validation and Error Handling

    3 min
  • 27

    Protecting Admin Screens

    3 min
  • 28

    Production Build Pipeline

    3 min
  • 29

    Debugging React in the WordPress Admin

    4 min
  • 30

    Building Search and Filter Functionality

    3 min
  • 31

    Internationalization in React

    3 min
  • 32

    Managing File Uploads via REST API

    3 min
  • 33

    Optimizing API Response Times

    3 min
  • 34

    Working with Date and Time in React

    3 min
  • 35

    Implementing Drag-and-Drop Sorting

    3 min
  • 36

    Creating Custom Hooks for API Logic

    3 min
  • 37

    Integrating with Gutenberg Blocks

    4 min
  • 38

    Handling Conflict Resolution

    4 min
  • 39

    Building a Modal Confirmation System

    Coming soon
  • 40

    Implementing Activity Logging

    Coming soon
  • 41

    Using Webpack Aliases

    Coming soon
  • 42

    Unit Testing API Endpoints

    Coming soon
  • 43

    Unit Testing React Components

    Coming soon
  • 44

    Handling Large Datasets with GraphQL

    Coming soon
  • 45

    Implementing Real-time Updates with Web

    Coming soon
  • View full course