Skip to content

Instantly share code, notes, and snippets.

@kindy61
Created April 9, 2010 03:36
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 kindy61/360874 to your computer and use it in GitHub Desktop.
Save kindy61/360874 to your computer and use it in GitHub Desktop.
$.each(
{
Y: 'FullYear',
m: 'Month',
d: 'Date',
D: 'Day',
H: 'Hours',
M: 'Minutes',
S: 'Seconds',
s: 'Milliseconds',
t: 'Time'
},
function(k, v){
Date.prototype[k] =
(function(s,g){
return function(v) {
if (v!==undefined) {
this[s](v);
return this;
} else {
return this[g]();
}
}
})('set' + v, 'get' + v);
}
);
Date.prototype.fmt = function(s){
var d = this;
function x(m) {
var v = d[m]();
(m == 'm') && v++;
('mdHMS'.indexOf(m) > -1) && (v < 10) && (v = '0' + v);
return v;
}
return s.replace(/%./g, function(m){
m = m.charAt(1);
return m=='%' ? m :
m in d ? x(m) :
'*';
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment