Skip to content

Instantly share code, notes, and snippets.

@atesgoral
Forked from 140bytes/LICENSE.txt
Last active January 26, 2017 18:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save atesgoral/1005948 to your computer and use it in GitHub Desktop.
Save atesgoral/1005948 to your computer and use it in GitHub Desktop.
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!

(
d, // Date instance
f // Format string
) => f.replace( // Replace all tokens
/{(.+?):?(\d*)}/g, // {<part>:<padding>}
(
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
})
(d,f)=>f.replace(/{(.+?):?(\d*)}/g,(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 = (d,f)=>f.replace(/{(.+?):?(\d*)}/g,(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
Copy link

subzey commented Jun 3, 2011

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
Copy link
Author

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

@subzey
Copy link

subzey commented Jun 3, 2011

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
Copy link
Author

@subzey: Thank you sir! Updating...

@Alexei-B
Copy link

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.

@atesgoral
Copy link
Author

@Alexei-B Thanks for the regexp suggestion. On top of those 4 characters, I shaved a whole bunch of characters by using arrow functions.

With regards to .toUTCString(), I think you missed the point. This is a generic formatter that allows you to format dates in any way you like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment