public
Last active — forked from 140bytes/LICENSE.txt

Date formatter that uses human-readable date instance getters instead of cryptic Y/M/D etc. tokens

  • Download Gist
LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
 
Copyright (C) 2011 Ates Goral <http://magnetiq.com>
 
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
 
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 
0. You just DO WHAT THE FUCK YOU WANT TO.
README.md
Markdown

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!

annotated.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
function (
d, // Date instance
f // Format string
) {
return f.replace( // Replace all tokens
/{(.+?)(?::(.*?))?}/g, // {<part>:<padding>}
function (
v, // Matched string (ignored, used as local var)
c, // Date component name
p // Padding amount
) {
for(v = d["get" + c]() // Execute date component getter
+ /h/.test(c) // Increment Mont(h) components by 1
+ ""; // Cast to String
v.length < p; // While padding needed,
v = 0 + v); // pad with zeros
return v // Return padded result
})
}
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12
{
"name": "formatDate",
 
"description": "Date formatter",
 
"keywords": [
"date",
"string",
"formatting",
"conversion"
]
}
test.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>2011-06-03 02:33:54</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var formatDate = function(d,f){return f.replace(/{(.+?)(?::(.*?))?}/g,function(v,c,p){for(v=d["get"+c]()+/h/.test(c)+"";v.length<p;v=0+v);return v})};
 
document.getElementById( "ret" ).innerHTML = formatDate(
new Date(2011, 5, 3, 2, 33, 54, 0),
"{FullYear}-{Month:2}-{Date:2} {Hours:2}:{Minutes:2}:{Seconds:2}"
);
</script>

Great work!

As far as I understand, you may use
… function(v,c,p){v=d["get"+c] …
instead of
… function(s,c,p,v){v=d["get"+c] …
Anyway, first argument is unused and v is assigned explicitly.

@subzey: Why didn't I think of that!? Thanks! Shaved 2 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}

@subzey: Thank you sir! Updating...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.