Instead of using classical format specifiers like "YYYY", "MM", "HH", "mm" etc., this function uses the Date instance getters like getFullYear
, getMonth
, etc., with support for zero-padding.
For example, instead of:
"YYYY-MM-DD HH:mm:ss"
you can use:
"{FullYear}-{Month:2}-{Date:2} {Hours:2}:{Minutes:2}:{Seconds:2}"
Verbose, but more human-friendly. And can be done within 140 bytes!
One more I noticed: in this case for loop
for(;v.length<p;v=0+v);return v)
is shorter than
return Array(p-v.length+1).join(0)+v
(and cannot produce errors with creating arrays of negative length)
Also, this allows to reduce the whole code further by removing one semicolon:
function(v,c,p){for(v=d["get"+c]()+/h/.test(c)+"";v.length<p;v=0+v);return v}