Design & Download
Your JS Library
in Seconds!!!

Snippet: formatDate() - Date Formatter

Written by Christopher West (cwest) on December 06, 2015.
Creates a string representation of the date using the specified format string.
var formatDate;
(function(dayNames, monthNames, RGX_FORMAT, RGX_QUOTE, RGX_2_CHARS) {
  formatDate = function(date, format, opt_dayNames, opt_monthNames) {
    return format.replace(RGX_FORMAT, function(str) {
      var c1 = str.charAt(0),
          ret = str.charAt(0) == "'"
          ? (c1=0) || str.slice(1, -1).replace(RGX_QUOTE, "'")
          : str == "a"
            ? (date.getHours() < 12 ? "am" : "pm")
            : str == "A"
              ? (date.getHours() < 12 ? "AM" : "PM")
              : str == "Z"
                ? (("+" + -date.getTimezoneOffset() / 60).replace('+-', "-").replace(RGX_2_CHARS, "$10$2") + "00")
                : c1 == "S"
                  ? date.getMilliseconds()
                  : c1 == "s"
                    ? date.getSeconds()
                    : c1 == "H"
                      ? date.getHours()
                      : c1 == "h"
                        ? (date.getHours() % 12) || 12
                        : (c1 == "D" && str.length > 2)
                          ? (opt_dayNames || dayNames)[date.getDay()].slice(0, str.length > 3 ? 9 : 3)
                          : c1 == "D"
                            ? date.getDate()
                            : (c1 == "M" && str.length > 2)
                              ? (opt_monthNames || monthNames)[date.getMonth()].slice(0, str.length > 3 ? 9 : 3)
                              : c1 == "m"
                                ? date.getMinutes()
                                : c1 == "M"
                                  ? date.getMonth() + 1
                                  : ("" + date.getFullYear()).slice(-str.length);
      return c1 && str.length < 4 && ("" + ret).length < str.length
        ? ("00" + ret).slice(-str.length)
        : ret;
    });
  };
})(
  "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(","),
  "January,February,March,April,May,June,July,August,September,October,November,December".split(","),
  /a|A|Z|S(SS)?|ss?|mm?|HH?|hh?|D{1,4}|M{1,4}|YY(YY)?|'([^']|'')*'/g,
  /''/g,
  /^(.)(.)$/
);

formatDate(...) API Documentation

Description

Creates a string representation of the date using the specified format string.

Parameters

  1. date {Date}:
    The date to be represented as a string.
  2. format {string}:
    The format string indicating how to represent date as a string. A combination of any of the formats in the follow table can be used to get the desired result:
    Format Output Meaning
    YYYY 2012 Four-digit representation of the year.
    YY 12 Two-digit representation of the year.
    MMMM September Full textual representation of the month.
    MMM Sep Three letter representation of the month.
    MM 09 Month with the leading zero (two digits long).
    M 9 Month without the leading zero.
    DDDD Wednesday Full textual representation of the day of the week.
    DDD Wed Three letter representation of the day of the week.
    DD 03 Day of the month with leading zero (two digits long).
    D 3 Day of the month without leading zeros.
    HH 19 24-hour format of hour with leading zero (two digits long).
    hh 07 12-hour format of hour with leading zero (two digits long).
    H 19 24-hour format of hour without leading zeros.
    h 7 12-hour format of hour without leading zeros.
    mm 01 Minutes with the leading zero (two digits long).
    m 1 Minutes without the leading zero.
    ss 08 Seconds with the leading zero (two digits long).
    s 8 Seconds without the leading zero.
    a pm Lowercase am or pm.
    A PM Uppercase AM or PM.
    SSS 095 Milliseconds with leading zeros (three digits long).
    S 95 Milliseconds without leading zeros.
    Z -0400 Difference to Greenwich time (GMT) in hours.
    'NO ''FORMAT'' HERE' NO 'FORMAT' HERE The specified string within the single quotes printed literally. To escape a single quote, you must prepend it with another single quote.
    Date Used: Wednesday September 3, 2012 19:01:08.095 GMT-0400 (EDT)
  3. opt_dayNames {Array.<string>}:
    Optional. Defaults to ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]. If given, the day names in this array will be used as the name of the corresponding day in the returned string.
  4. opt_monthNames {Array.<string>}:
    Optional. Defaults to ["January","February","March","April","May","June","July","August","September","October","November","December"]. If given, the month names in this array will be used as the name of the corresponding month in the returned string.

Returns

The date formatted as specified by format.