Format Dates with the "format" function

NimbleText helps you write dates in whatever format you want.

You want to say "Sun, 19th May 2024." you use a pattern like this:

<% (new Date()).format("ddd, ddS MMM yyyy.") %>

with result:

Sun, 19th May 2024.

Note: while the format function converts a Date into a string, the reverse is also possible! Use the toDate function to turn any string into a date.

(Read this article to convert a string into a date)

The format function has two optional parameters, the mask and a boolean to indicate UTC (or not).

Mask Specifiers for the format function

Here's a description of all the mask specifiers you can use for formatting a date:

Mask Description
Day d Day of the month as digits; no leading zero for single-digit days.
dd Day of the month as digits; leading zero for single-digit days.
ddd Day of the week as a three-letter abbreviation (e.g. "Mon").
dddd Day of the week as its full name (e.g. "Monday").
Month M Month as digits, with no leading zero for single-digit months (e.g. '1' for January).
MM Month as digits, with a leading zero for single-digit months (e.g. '01' for January).
MMM Month as a three-letter abbreviation (e.g. 'Jan').
MMMM Month as its full name (e.g. 'January'.)
Year yy Year as last two digits; leading zero for years less than 10, (e.g. '24').
yyyy Year represented by four digits (e.g. '2024').
Hour h Hours; no leading zero for single-digit hours (12-hour clock, 1-12), (e.g. '1').
hh Hours; leading zero for single-digit hours (12-hour clock, 01-12), (e.g. '01').
H Hours; no leading zero for single-digit hours (24-hour clock, 0-23), (e.g. '3').
HH Hours; leading zero for single-digit hours (24-hour clock, 00-23), (e.g. '03').
Minutes m Minutes; no leading zero for single-digit minutes.
mm Minutes; leading zero for single-digit minutes.
Seconds s Seconds; no leading zero for single-digit seconds.
ss Seconds; leading zero for single-digit seconds.
Milliseconds l Milliseconds to 3 digits.
L Milliseconds to 2 digits.
AM/PM t Lowercase, single-character time marker string: 'a' or 'p'.
tt Lowercase, two-character time marker string: 'am' or 'pm'.
T Uppercase, single-character time marker string: 'A' or 'P.
TT Uppercase, two-character time marker string: 'AM' or 'PM'.
Timezone Z US timezone abbreviation, e.g. 'EST' or 'MDT'. With non-US timezones or in the Opera browser, the GMT/UTC offset is returned, e.g. GMT-0500.
o GMT/UTC timezone offset, e.g. -0500 or +0230.
UTC: If present, 'UTC:' must be the first four characters of the mask. Converts the date from local time to UTC/GMT/Zulu time before applying the mask. The "UTC:" prefix is removed.
Other S The date's ordinal suffix (st, nd, rd, or th). Works well with d.
'…' or "…" Literal character sequence. Surrounding quotes are removed. e.g. '"Next week:"'

 

Named Masks

There are also some named masks that you can use. These are not currently configurable. If you'd like to be able to edit the defined masks used by these masks, it can be included in a future version of NimbleText. You need only ask.

Name Mask Example
default ddd MMM dd yyyy HH:mm:ss Sun May 19 2024 17:02:44
Sat Jun 09 2007 17:46:21
shortDate M/d/yy 5/19/24
6/9/07
mediumDate MMM d, yyyy May 19, 2024
Jun 9, 2007
longDate MMMM d, yyyy May 19, 2024
June 9, 2007
fullDate dddd, MMMM d, yyyy Sunday, May 19, 2024
Saturday, June 9, 2007
shortTime h:mm TT 5:02 PM
5:46 PM
mediumTime h:mm:ss TT 5:02:44 PM
5:46:21 PM
longTime h:mm:ss TT Z 5:02:44 PM EST
5:46:21 PM EST
isoDate yyyy-MM-dd 2024-05-19
2007-06-09
isoTime HH:mm:ss 17:02:44
17:46:21
isoDateTime yyyy-MM-dd'T'HH:mm:ss 2024-05-19T17:02:44
2007-06-09T17:46:21
isoUtcDateTime UTC:yyyy-MM-dd'T'HH:mm:ss'Z' 2024-05-19T17:02:44Z
2007-06-09T22:46:21Z

 

This feature is only available in the Desktop version. (Compare versions).

 

Here are some useful formats that I need to check the support for

Name Mask Example
???? h:mm:ss TT ??? 5:02:44 PM +0
5:02:44 PM +00:00
5:46:21 PM -03:00

Acknowledgement and Thanks

This functionality is provided by Steven Levithan's excellent Date-time format library. The main underlying difference is that in NimbleText capital 'M' is used for month, while lower-case 'm' for minutes (instead of the opposite); this is done for consistency with C# (and dotnet in general) where myself and many of my customers are most familiar.

The documentation above is based on the original JavaScript Date Format Documentation from Steven Levithan with some changes, so errors are all mine.

Steven is also the author of the cross-browser split function which is used by NimbleText. He co-authored O'Reilly's Regular Expressions Cookbook (now in its second edition) so I think you should go and buy that.

Internationalization

Note that internationalization can also be achieved (though I don't have any special feature to make this smoother, yet)

As described by Steven Levithan here, you can specify the substrings used for parts of dates, like so:

<%
dateFormat.i18n = {
  dayNames: [
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  ],
  monthNames: [
    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  ]
}; %>
XKCD Cartoon about ISO 8601

xkcd/1179 (also see Doeke Zanstra's live clock based on this XKCD cartoon, and xkcd/now)

Further help

You can also get general help, help on all the symbols and keywords, or on the built-in functions, filtering with a where clause, help with the powerful command-line automation, or applying custom formats to your dates and times.

You need to purchase a license to unlock all the features in NimbleText.

If you haven't downloaded NimbleText yet, then for added power, privacy and versatility I sincerely think you should download it now.

Download NimbleText