Skip to content

Instantly share code, notes, and snippets.

@chris-roerig
Created March 4, 2015 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-roerig/c62ad21384c56aad1ea7 to your computer and use it in GitHub Desktop.
Save chris-roerig/c62ad21384c56aad1ea7 to your computer and use it in GitHub Desktop.
Javascript date.toString()
//Extracted from https://code.google.com/p/datejs/source/browse/trunk/src/core.js#761
/**
* Converts the value of the current Date object to its equivalent string representation.
* Format Specifiers
<pre>
CUSTOM DATE AND TIME FORMAT STRINGS
Format Description Example
------ --------------------------------------------------------------------------- -----------------------
s The seconds of the minute between 0-59. "0" to "59"
ss The seconds of the minute with leading zero if required. "00" to "59"
m The minute of the hour between 0-59. "0" or "59"
mm The minute of the hour with leading zero if required. "00" or "59"
h The hour of the day between 1-12. "1" to "12"
hh The hour of the day with leading zero if required. "01" to "12"
H The hour of the day between 0-23. "0" to "23"
HH The hour of the day with leading zero if required. "00" to "23"
d The day of the month between 1 and 31. "1" to "31"
dd The day of the month with leading zero if required. "01" to "31"
ddd Abbreviated day name. $C.abbreviatedDayNames. "Mon" to "Sun"
dddd The full day name. $C.dayNames. "Monday" to "Sunday"
M The month of the year between 1-12. "1" to "12"
MM The month of the year with leading zero if required. "01" to "12"
MMM Abbreviated month name. $C.abbreviatedMonthNames. "Jan" to "Dec"
MMMM The full month name. $C.monthNames. "January" to "December"
yy The year as a two-digit number. "99" or "08"
yyyy The full four digit year. "1999" or "2008"
t Displays the first character of the A.M./P.M. designator. "A" or "P"
$C.amDesignator or $C.pmDesignator
tt Displays the A.M./P.M. designator. "AM" or "PM"
$C.amDesignator or $C.pmDesignator
S The ordinal suffix ("st, "nd", "rd" or "th") of the current day. "st, "nd", "rd" or "th"
|| *Format* || *Description* || *Example* ||
|| d || The CultureInfo shortDate Format Pattern || "M/d/yyyy" ||
|| D || The CultureInfo longDate Format Pattern || "dddd, MMMM dd, yyyy" ||
|| F || The CultureInfo fullDateTime Format Pattern || "dddd, MMMM dd, yyyy h:mm:ss tt" ||
|| m || The CultureInfo monthDay Format Pattern || "MMMM dd" ||
|| r || The CultureInfo rfc1123 Format Pattern || "ddd, dd MMM yyyy HH:mm:ss GMT" ||
|| s || The CultureInfo sortableDateTime Format Pattern || "yyyy-MM-ddTHH:mm:ss" ||
|| t || The CultureInfo shortTime Format Pattern || "h:mm tt" ||
|| T || The CultureInfo longTime Format Pattern || "h:mm:ss tt" ||
|| u || The CultureInfo universalSortableDateTime Format Pattern || "yyyy-MM-dd HH:mm:ssZ" ||
|| y || The CultureInfo yearMonth Format Pattern || "MMMM, yyyy" ||
STANDARD DATE AND TIME FORMAT STRINGS
Format Description Example ("en-US")
------ --------------------------------------------------------------------------- -----------------------
d The CultureInfo shortDate Format Pattern "M/d/yyyy"
D The CultureInfo longDate Format Pattern "dddd, MMMM dd, yyyy"
F The CultureInfo fullDateTime Format Pattern "dddd, MMMM dd, yyyy h:mm:ss tt"
m The CultureInfo monthDay Format Pattern "MMMM dd"
r The CultureInfo rfc1123 Format Pattern "ddd, dd MMM yyyy HH:mm:ss GMT"
s The CultureInfo sortableDateTime Format Pattern "yyyy-MM-ddTHH:mm:ss"
t The CultureInfo shortTime Format Pattern "h:mm tt"
T The CultureInfo longTime Format Pattern "h:mm:ss tt"
u The CultureInfo universalSortableDateTime Format Pattern "yyyy-MM-dd HH:mm:ssZ"
y The CultureInfo yearMonth Format Pattern "MMMM, yyyy"
</pre>
* @param {String} A format string consisting of one or more format spcifiers [Optional].
* @return {String} A string representation of the current Date object.
*/
(function () {
var $D = Date,
$P = $D.prototype,
$C = $D.CultureInfo,
p = function (s, l) {
if (!l) {
l = 2;
}
return ("000" + s).slice(l * -1);
};
$P.toString = function (format) {
var x = this;
// Standard Date and Time Format Strings. Formats pulled from CultureInfo file and
// may vary by culture.
if (format && format.length == 1) {
var c = $C.formatPatterns;
x.t = x.toString;
switch (format) {
case "d":
return x.t(c.shortDate);
case "D":
return x.t(c.longDate);
case "F":
return x.t(c.fullDateTime);
case "m":
return x.t(c.monthDay);
case "r":
return x.t(c.rfc1123);
case "s":
return x.t(c.sortableDateTime);
case "t":
return x.t(c.shortTime);
case "T":
return x.t(c.longTime);
case "u":
return x.t(c.universalSortableDateTime);
case "y":
return x.t(c.yearMonth);
}
}
var ord = function (n) {
switch (n * 1) {
case 1:
case 21:
case 31:
return "st";
case 2:
case 22:
return "nd";
case 3:
case 23:
return "rd";
default:
return "th";
}
};
return format ? format.replace(/(\\)?(dd?d?d?|MM?M?M?|yy?y?y?|hh?|HH?|mm?|ss?|tt?|S)/g,
function (m) {
if (m.charAt(0) === "\\") {
return m.replace("\\", "");
}
x.h = x.getHours;
switch (m) {
case "hh":
return p(x.h() < 13 ? (x.h() === 0 ? 12 : x.h()) : (x.h() - 12));
case "h":
return x.h() < 13 ? (x.h() === 0 ? 12 : x.h()) : (x.h() - 12);
case "HH":
return p(x.h());
case "H":
return x.h();
case "mm":
return p(x.getMinutes());
case "m":
return x.getMinutes();
case "ss":
return p(x.getSeconds());
case "s":
return x.getSeconds();
case "yyyy":
return p(x.getFullYear(), 4);
case "yy":
return p(x.getFullYear());
case "dddd":
return $C.dayNames[x.getDay()];
case "ddd":
return $C.abbreviatedDayNames[x.getDay()];
case "dd":
return p(x.getDate());
case "d":
return x.getDate();
case "MMMM":
return $C.monthNames[x.getMonth()];
case "MMM":
return $C.abbreviatedMonthNames[x.getMonth()];
case "MM":
return p((x.getMonth() + 1));
case "M":
return x.getMonth() + 1;
case "t":
return x.h() < 12 ? $C.amDesignator.substring(0, 1) : $C.pmDesignator.substring(0, 1);
case "tt":
return x.h() < 12 ? $C.amDesignator : $C.pmDesignator;
case "S":
return ord(x.getDate());
default:
return m;
}
}
) : this._toString();
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment