dateformat is a Node.js package that provides Steven Levithan's excellent dateFormat() function for formatting dates with extensive customization options. It offers flexible formatting through customizable masks, timezone conversion, localization support, and comprehensive date/time representation options.
npm install dateformatimport dateFormat from "dateformat";
import dateFormat, { masks, i18n, formatTimezone } from "dateformat";For named imports only:
import { masks, i18n, formatTimezone } from "dateformat";import dateFormat, { masks } from "dateformat";
const now = new Date();
// Basic usage with current date and default mask
dateFormat();
// "Sat Jun 09 2007 17:46:21"
// Format specific date with custom mask
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// "Saturday, June 9th, 2007, 5:46:21 PM"
// Use predefined masks
dateFormat(now, "isoDateTime");
// "2007-06-09T17:46:21+0700"
// Add custom masks
masks.customTime = 'HH:MM! "Custom format"';
dateFormat(now, "customTime");
// "17:46! Custom format"
// UTC conversion
dateFormat(now, "longTime", true);
// "10:46:21 PM UTC"
// GMT prefix conversion
dateFormat(now, "GMT:h:MM:ss TT Z");
// "10:46:21 PM GMT"Core date formatting function that converts dates to formatted strings using customizable masks.
/**
* Formats a date using the specified mask
* @param {string | number | Date} [date] - Date to format, defaults to current date
* @param {string} [mask] - Format mask, defaults to masks.default
* @param {boolean} [utc] - Convert to UTC time
* @param {boolean} [gmt] - Display GMT timezone
* @returns {string} Formatted date string
* @throws {TypeError} When date is invalid
*/
function dateFormat(date, mask, utc, gmt);Usage Examples:
// Current date with default mask
dateFormat();
// Specific date with custom mask
dateFormat(new Date(2007, 5, 9), "yyyy-mm-dd");
// "2007-06-09"
// String date input
dateFormat("Jun 9 2007", "fullDate");
// "Saturday, June 9, 2007"
// UTC conversion
dateFormat(now, "isoDateTime", true);
// Mask-only call (uses current date)
dateFormat("shortTime");
// "5:46 PM"Predefined and customizable format masks for common date/time patterns.
/**
* Object containing predefined format masks
* @type {Object<string, string>}
*/
const masks = {
default: "ddd mmm dd yyyy HH:MM:ss",
shortDate: "m/d/yy",
paddedShortDate: "mm/dd/yyyy",
mediumDate: "mmm d, yyyy",
longDate: "mmmm d, yyyy",
fullDate: "dddd, mmmm d, yyyy",
shortTime: "h:MM TT",
mediumTime: "h:MM:ss TT",
longTime: "h:MM:ss TT Z",
isoDate: "yyyy-mm-dd",
isoTime: "HH:MM:ss",
isoDateTime: "yyyy-mm-dd'T'HH:MM:sso",
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'",
expiresHeaderFormat: "ddd, dd mmm yyyy HH:MM:ss Z"
};Usage Examples:
// Using predefined masks
dateFormat(date, "isoDate");
dateFormat(date, "longTime");
// Adding custom masks
masks.apiTimestamp = "yyyy-mm-dd HH:MM:ss";
dateFormat(date, "apiTimestamp");
// Accessing mask values
console.log(masks.default); // "ddd mmm dd yyyy HH:MM:ss"Localization settings for day names, month names, and AM/PM indicators.
/**
* Internationalization configuration object
* @type {Object}
*/
const i18n = {
/** Day names: short names (0-6) and full names (7-13) */
dayNames: [
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
],
/** Month names: short names (0-11) and full names (12-23) */
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"
],
/** Time markers: ["a", "p", "am", "pm", "A", "P", "AM", "PM"] */
timeNames: ["a", "p", "am", "pm", "A", "P", "AM", "PM"]
};Usage Examples:
// Localize to different language
i18n.dayNames = [
"Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab",
"Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"
];
i18n.monthNames = [
"Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic",
"Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"
];
// Format with localized names
dateFormat(date, "dddd, mmmm d, yyyy");
// "Domingo, Enero 15, 2023" (Spanish)Utility function for extracting and formatting timezone information from dates.
/**
* Extracts and formats timezone abbreviation or offset from a date
* @param {string | Date} date - Date object or string to extract timezone from
* @returns {string} Timezone abbreviation (e.g., "EST") or GMT offset (e.g., "GMT-0500")
*/
function formatTimezone(date);Usage Examples:
const date = new Date();
// Get timezone string
const tz = formatTimezone(date);
console.log(tz); // "EST" or "GMT-0500"
// Use in custom formatting
const customFormat = `${dateFormat(date, "yyyy-mm-dd HH:MM:ss")} ${formatTimezone(date)}`;Complete reference of all supported format tokens:
| Token | Description | Example |
|---|---|---|
d | Day of month, no leading zero | 1-31 |
dd | Day of month, leading zero | 01-31 |
ddd | Short day name | Sun, Mon, Tue |
DDD | Smart day name | Ysd, Tdy, Tmw, or ddd |
dddd | Full day name | Sunday, Monday |
DDDD | Smart full day name | Yesterday, Today, Tomorrow, or dddd |
m | Month, no leading zero | 1-12 |
mm | Month, leading zero | 01-12 |
mmm | Short month name | Jan, Feb, Mar |
mmmm | Full month name | January, February |
yy | Two-digit year | 07, 23 |
yyyy | Four-digit year | 2007, 2023 |
h | 12-hour format hour, no leading zero | 1-12 |
hh | 12-hour format hour, leading zero | 01-12 |
H | 24-hour format hour, no leading zero | 0-23 |
HH | 24-hour format hour, leading zero | 00-23 |
M | Minutes, no leading zero | 0-59 |
MM | Minutes, leading zero | 00-59 |
s | Seconds, no leading zero | 0-59 |
ss | Seconds, leading zero | 00-59 |
l | Milliseconds, 3 digits | 000-999 |
L | Milliseconds, 2 digits | 00-99 |
t | Lowercase single AM/PM | a, p |
tt | Lowercase double AM/PM | am, pm |
T | Uppercase single AM/PM | A, P |
TT | Uppercase double AM/PM | AM, PM |
Z | Timezone abbreviation | EST, GMT-0500 |
o | GMT offset format | -0500, +0230 |
p | GMT offset with colon | -05:00, +02:30 |
S | Ordinal suffix | st, nd, rd, th |
W | ISO week number | 1-53 |
WW | ISO week number, leading zero | 01-53 |
N | ISO day of week (Monday=1) | 1-7 |
'...', "..." | Literal text | Surrounding quotes removed |
UTC: | UTC prefix | Must be first 4 characters |
GMT: | GMT prefix | Must be first 4 characters |
The dateFormat function throws a TypeError with the message "Invalid date" when:
new Date("invalid"))try {
dateFormat("not a date", "yyyy-mm-dd");
} catch (error) {
console.log(error.message); // "Invalid date"
}All other invalid parameters are handled gracefully:
masks.defaultfalse