javascript:date_format
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
javascript:date_format [2016/07/07 20:08] – created peter | javascript:date_format [2020/07/15 09:30] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== JavaScript - Date format ====== | ====== JavaScript - Date format ====== | ||
+ | |||
+ | JavaScript provides a bunch of [[http:// | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | PHP uses a crazy mix of letters to represent various date entities. | ||
+ | |||
+ | ColdFusion on the other hand have conflicting letters where m and mm mean different things depending on whether you're dealing with dates or times. On the other hand, ColdFusion uses very easy to remember special characters for masks. | ||
+ | |||
+ | With my date formatter, I've tried to take the best features from both, and add some sugar of my own. It did end up a lot like the ColdFusion implementation though, since I've primarily used CF's mask syntax. | ||
+ | |||
<code javascript> | <code javascript> | ||
Line 50: | Line 61: | ||
// 10:46:21 PM UTC | // 10:46:21 PM UTC | ||
</ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ^Mask^Description^ | ||
+ | |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.| | ||
+ | |dddd|Day of the week as its full name.| | ||
+ | |m|Month as digits; no leading zero for single-digit months.| | ||
+ | |mm|Month as digits; leading zero for single-digit months.| | ||
+ | |mmm|Month as a three-letter abbreviation.| | ||
+ | |mmmm|Month as its full name.| | ||
+ | |yy|Year as last two digits; leading zero for years less than 10.| | ||
+ | |yyyy|Year represented by four digits.| | ||
+ | |h|Hours; no leading zero for single-digit hours (12-hour clock).| | ||
+ | |hh|Hours; leading zero for single-digit hours (12-hour clock).| | ||
+ | |H|Hours; no leading zero for single-digit hours (24-hour clock).| | ||
+ | |HH|Hours; leading zero for single-digit hours (24-hour clock).| | ||
+ | |M|Minutes; no leading zero for single-digit minutes. Uppercase M unlike CF timeFormat' | ||
+ | |MM|Minutes; | ||
+ | |s|Seconds; no leading zero for single-digit seconds.| | ||
+ | |ss|Seconds; | ||
+ | |l or L|Milliseconds. l gives 3 digits. L gives 2 digits.| | ||
+ | |t|Lowercase, | ||
+ | |tt|Lowercase, | ||
+ | |T|Uppercase, | ||
+ | |TT|Uppercase, | ||
+ | |Z|US timezone abbreviation, | ||
+ | No equivalent in CF.| | ||
+ | |o|GMT/UTC timezone offset, e.g. -0500 or +0230. | ||
+ | |S|The date's ordinal suffix (st, nd, rd, or th). Works well with d. No equivalent in CF.| | ||
+ | |' | ||
+ | |UTC:|Must be the first four characters of the mask.// Converts the date from local time to UTC/ | ||
+ | |||
+ | |||
+ | ===== Named Masks (provided by default) ===== | ||
+ | |||
+ | |||
+ | It is easy to change these or add your own. | ||
+ | |||
+ | ^Name^Mask^Example^ | ||
+ | |default|ddd mmm dd yyyy HH: | ||
+ | |shortDate|m/ | ||
+ | |mediumDate|mmm d, yyyy|Jun 9, 2007| | ||
+ | |longDate|mmmm d, yyyy|June 9, 2007| | ||
+ | |fullDate|dddd, | ||
+ | |shortTime|h: | ||
+ | |mediumTime|h: | ||
+ | |longTime|h: | ||
+ | |isoDate|yyyy-mm-dd|2007-06-09| | ||
+ | |isoTime|HH: | ||
+ | |isoDateTime|yyyy-mm-dd' | ||
+ | |isoUtcDateTime|UTC: | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Library ===== | ||
+ | |||
+ | See http:// | ||
+ | |||
+ | <code javascript> | ||
+ | /* | ||
+ | * Date Format 1.2.3 | ||
+ | * (c) 2007-2009 Steven Levithan < | ||
+ | * MIT license | ||
+ | * | ||
+ | * Includes enhancements by Scott Trenda < | ||
+ | * and Kris Kowal < | ||
+ | * | ||
+ | * Accepts a date, a mask, or a date and a mask. | ||
+ | * Returns a formatted version of the given date. | ||
+ | * The date defaults to the current date/time. | ||
+ | * The mask defaults to dateFormat.masks.default. | ||
+ | */ | ||
+ | |||
+ | var dateFormat = function () { | ||
+ | var token = / | ||
+ | timezone = / | ||
+ | timezoneClip = / | ||
+ | pad = function (val, len) { | ||
+ | val = String(val); | ||
+ | len = len || 2; | ||
+ | while (val.length < len) val = " | ||
+ | return val; | ||
+ | }; | ||
+ | |||
+ | // Regexes and supporting functions are cached through closure | ||
+ | return function (date, mask, utc) { | ||
+ | var dF = dateFormat; | ||
+ | |||
+ | // You can't provide utc if you skip other args (use the " | ||
+ | if (arguments.length == 1 && Object.prototype.toString.call(date) == " | ||
+ | mask = date; | ||
+ | date = undefined; | ||
+ | } | ||
+ | |||
+ | // Passing date through Date applies Date.parse, if necessary | ||
+ | date = date ? new Date(date) : new Date; | ||
+ | if (isNaN(date)) throw SyntaxError(" | ||
+ | |||
+ | mask = String(dF.masks[mask] || mask || dF.masks[" | ||
+ | |||
+ | // Allow setting the utc argument via the mask | ||
+ | if (mask.slice(0, | ||
+ | mask = mask.slice(4); | ||
+ | utc = true; | ||
+ | } | ||
+ | |||
+ | var _ = utc ? " | ||
+ | d = date[_ + " | ||
+ | D = date[_ + " | ||
+ | m = date[_ + " | ||
+ | y = date[_ + " | ||
+ | H = date[_ + " | ||
+ | M = date[_ + " | ||
+ | s = date[_ + " | ||
+ | L = date[_ + " | ||
+ | o = utc ? 0 : date.getTimezoneOffset(), | ||
+ | flags = { | ||
+ | d: d, | ||
+ | dd: pad(d), | ||
+ | ddd: dF.i18n.dayNames[D], | ||
+ | dddd: dF.i18n.dayNames[D + 7], | ||
+ | m: m + 1, | ||
+ | mm: pad(m + 1), | ||
+ | mmm: dF.i18n.monthNames[m], | ||
+ | mmmm: dF.i18n.monthNames[m + 12], | ||
+ | yy: String(y).slice(2), | ||
+ | yyyy: y, | ||
+ | h: H % 12 || 12, | ||
+ | hh: pad(H % 12 || 12), | ||
+ | H: H, | ||
+ | HH: pad(H), | ||
+ | M: M, | ||
+ | MM: pad(M), | ||
+ | s: s, | ||
+ | ss: pad(s), | ||
+ | l: pad(L, 3), | ||
+ | L: pad(L > 99 ? Math.round(L / 10) : L), | ||
+ | t: H < 12 ? " | ||
+ | tt: H < 12 ? " | ||
+ | T: H < 12 ? " | ||
+ | TT: H < 12 ? " | ||
+ | Z: utc ? " | ||
+ | o: (o > 0 ? " | ||
+ | S: [" | ||
+ | }; | ||
+ | |||
+ | return mask.replace(token, | ||
+ | return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1); | ||
+ | }); | ||
+ | }; | ||
+ | }(); | ||
+ | |||
+ | |||
+ | // Some common format strings | ||
+ | dateFormat.masks = { | ||
+ | " | ||
+ | shortDate: | ||
+ | mediumDate: | ||
+ | longDate: | ||
+ | fullDate: | ||
+ | shortTime: | ||
+ | mediumTime: | ||
+ | longTime: | ||
+ | isoDate: | ||
+ | isoTime: | ||
+ | isoDateTime: | ||
+ | isoUtcDateTime: | ||
+ | }; | ||
+ | |||
+ | |||
+ | // Internationalization strings | ||
+ | dateFormat.i18n = { | ||
+ | dayNames: [ | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | monthNames: [ | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | }; | ||
+ | |||
+ | |||
+ | // For convenience... | ||
+ | Date.prototype.format = function (mask, utc) { | ||
+ | return dateFormat(this, | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | Note that the day and month names can be changed (for internationalization or other purposes) by updating the **dateFormat.i18n** object. |
javascript/date_format.1467922085.txt.gz · Last modified: 2020/07/15 09:30 (external edit)