javascript:date_format
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
javascript:date_format [2016/07/07 20:25] – peter | javascript:date_format [2020/07/15 09:30] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 3: | Line 3: | ||
JavaScript provides a bunch of [[http:// | JavaScript provides a bunch of [[http:// | ||
- | [[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. | ||
Line 110: | Line 116: | ||
+ | |||
+ | |||
+ | ===== 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.1467923133.txt.gz · Last modified: 2020/07/15 09:30 (external edit)