Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Date formatter that uses human-readable date instance getters instead of cryptic Y/M/D etc. tokens

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!

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
})
}
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})}
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.
{
"name": "formatDate",
"description": "Date formatter",
"keywords": [
"date",
"string",
"formatting",
"conversion"
]
}
<!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>
@subzey

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.

@atesgoral
Owner

@subzey: Why didn't I think of that!? Thanks! Shaved 2 bytes.

@subzey

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}

@atesgoral
Owner

@subzey: Thank you sir! Updating...

@Alexei-B

You can drop 4 bytes by changing the regex;

/{(.+?)(?::(.*?))?}/g // What an ugly regexp!
/{(.+?):*(\d*)}/g     // Bathe in my regexp glory!

Also you can do basically the same thing with;

return d.toUTCString()

Fun officially ruined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.