Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You prototype a method so you never have to do this irritating task again:</p> <pre><code>Date.prototype.toFormattedString = function (f) { var nm = this.getMonthName(); var nd = this.getDayName(); f = f.replace(/yyyy/g, this.getFullYear()); f = f.replace(/yy/g, String(this.getFullYear()).substr(2,2)); f = f.replace(/MMM/g, nm.substr(0,3).toUpperCase()); f = f.replace(/Mmm/g, nm.substr(0,3)); f = f.replace(/MM\*/g, nm.toUpperCase()); f = f.replace(/Mm\*/g, nm); f = f.replace(/mm/g, String(this.getMonth()+1).padLeft('0',2)); f = f.replace(/DDD/g, nd.substr(0,3).toUpperCase()); f = f.replace(/Ddd/g, nd.substr(0,3)); f = f.replace(/DD\*/g, nd.toUpperCase()); f = f.replace(/Dd\*/g, nd); f = f.replace(/dd/g, String(this.getDate()).padLeft('0',2)); f = f.replace(/d\*/g, this.getDate()); return f; }; </code></pre> <p><em>(and yes you could chain those replaces, but it's not here for readability before anyone asks)</em></p> <hr> <p>As requested, additional prototypes to support the above snippet.</p> <pre><code>Date.prototype.getMonthName = function () { return this.toLocaleString().replace(/[^a-z]/gi,''); }; //n.b. this is sooo not i18n safe :) Date.prototype.getDayName = function () { switch(this.getDay()) { case 0: return 'Sunday'; case 1: return 'Monday'; case 2: return 'Tuesday'; case 3: return 'Wednesday'; case 4: return 'Thursday'; case 5: return 'Friday'; case 6: return 'Saturday'; } }; String.prototype.padLeft = function (value, size) { var x = this; while (x.length &lt; size) {x = value + x;} return x; }; </code></pre> <p>and usage example:</p> <pre><code>alert((new Date()).toFormattedString('dd Mmm, yyyy')); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload