204

I have a format of "Jan. 27, 2015 at 8:17 AM" that I need to display using moment.js. I'm using the format

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

Everything works great except for the word "at". How can I get that word to display as a word instead of the "a" in "at" being translated to the "am/pm". Right now using that date format it ends up looking like this: Jan. 27, 2015 amt 8:17 AM. Notice the "amt" instead of "at".

Is there any simple way of getting it to not process the "a" as part of the format? I have already tried splitting the output and manually entering the "at" after the third space but I'd like a cleaner code if possible.

1 Answer 1

519

Escape it with square braces

moment().format('MMM. D, YYYY [at] h:mm A z');
// produces:    "Jan. 30, 2015 at 2:46 PM "
Sign up to request clarification or add additional context in comments.

6 Comments

That did it! I knew there had to be a way :) Thanks
Just one note, make sure you have space between the custom text and the format tokens
What if this text is a variable defined as const. Can we include it?
How can you add i18n into the mix and translate [at]? So that e.g. moment().locale('en').format('MMM. D, YYYY [at] h:mm A z'); returns "Jul. 27, 2020 at 12:54 PM " whereas moment().locale('it').format('MMM. D, YYYY [at] h:mm A z'); returns "lug. 27, 2020 alle 12:54 PM "? How can we achieve this? Thank you!
Might be a bit late but what I do to work with custom format based on the locale is that I put the moment string format directly into my i18n's locale json, so it will load a different string format based on the language I suppose another way would be to just create a string out of pieces if your general format is the same, like 'MMM. D, YYYY [at] h:mm A z' could become 'MMM. D, YYYY [' + i18n('at') + '] h:mm A z' or for magic quote user `MMM. D, YYYY [${i18n('at')}] h:mm A z`
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.