Clue Mediator

Include text in the middle of Date format with Moment.js

📅June 12, 2022

In this short article, we will show you how to include text in the middle of Date format with Moment.js. Refer to the list of articles that are based on the Moment.js.

Checkout more articles on JavaScript

Let’s assume that we need to use moment.js to display a date in the format `Jan. 26, 2022 at 9:17 AM`. This is the format we usually use.

moment().format('MMM. D, YYYY at h:mm A z');

Except for the term `at`, everything works well. How can we make that word appear as a word rather than the `a` in `at` being converted to `am/pm`? Using that date format, it now looks like this: `Jan. 26, 2022 amt 9:17 AM`. The `amt` is used instead of `at`.

Solution:

Using the Moment JS we can Escape the word with square braces. Look at the following syntax to get the desired output.

moment().format('MMM. D, YYYY [at] h:mm A z');
// Output: Jan. 26, 2022 at 9:17 AM

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂