or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

dateformat

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.

Package Information

  • Package Name: dateformat
  • Package Type: npm
  • Language: JavaScript (ES modules)
  • Installation: npm install dateformat

Core Imports

import dateFormat from "dateformat";
import dateFormat, { masks, i18n, formatTimezone } from "dateformat";

For named imports only:

import { masks, i18n, formatTimezone } from "dateformat";

Basic Usage

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"

Capabilities

Main Date Formatting Function

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"

Format Masks Configuration

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"

Internationalization Configuration

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)

Timezone Formatting

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)}`;

Format Tokens

Complete reference of all supported format tokens:

TokenDescriptionExample
dDay of month, no leading zero1-31
ddDay of month, leading zero01-31
dddShort day nameSun, Mon, Tue
DDDSmart day nameYsd, Tdy, Tmw, or ddd
ddddFull day nameSunday, Monday
DDDDSmart full day nameYesterday, Today, Tomorrow, or dddd
mMonth, no leading zero1-12
mmMonth, leading zero01-12
mmmShort month nameJan, Feb, Mar
mmmmFull month nameJanuary, February
yyTwo-digit year07, 23
yyyyFour-digit year2007, 2023
h12-hour format hour, no leading zero1-12
hh12-hour format hour, leading zero01-12
H24-hour format hour, no leading zero0-23
HH24-hour format hour, leading zero00-23
MMinutes, no leading zero0-59
MMMinutes, leading zero00-59
sSeconds, no leading zero0-59
ssSeconds, leading zero00-59
lMilliseconds, 3 digits000-999
LMilliseconds, 2 digits00-99
tLowercase single AM/PMa, p
ttLowercase double AM/PMam, pm
TUppercase single AM/PMA, P
TTUppercase double AM/PMAM, PM
ZTimezone abbreviationEST, GMT-0500
oGMT offset format-0500, +0230
pGMT offset with colon-05:00, +02:30
SOrdinal suffixst, nd, rd, th
WISO week number1-53
WWISO week number, leading zero01-53
NISO day of week (Monday=1)1-7
'...', "..."Literal textSurrounding quotes removed
UTC:UTC prefixMust be first 4 characters
GMT:GMT prefixMust be first 4 characters

Error Handling

The dateFormat function throws a TypeError with the message "Invalid date" when:

  • The date parameter cannot be parsed as a valid date
  • A Date object is invalid (e.g., 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:

  • Missing date defaults to current date
  • Missing mask defaults to masks.default
  • Invalid mask tokens are treated as literal text
  • Boolean flags default to false