JavaScript date string formatting
Posted on 15th of July 2008Date string formatting is very brutal in JavaScript, which probably has the worst built in library in relation to the distribution this language sees. So a long time ago I whipped up a script file, for doing just this. I always though PHP’s date function looked very nice, so I decided to model my function upon that.
Download: formatDate.js (13kB)
Supported switches and test
I’m not going to duplicate PHP’s documentation. But I will list briefly the supported switches. Basicly, switches should return the same as in PHP, if they don’t, it’s most likely in error, so let me know at .
| Supported switches | Unsupported switches | Currently unsupported, but might be included in the future |
| a, A, B, c, d, D, F, g, G, h, H, i, I (uppercase i), j, l (lowecase L), L, m, M, n, N, O, P, r, s, S, t, U, w, W, y, Y, z, Z | T, e, o |
Here’s some examples.
| Example | Output |
F j, Y, g:i a |
|
m.d.y |
|
j, n, Y |
|
Ymd |
|
h-i-s, j-m-y, it is w Day z |
|
%i%t %i%s %t%h%e jS %d%a%y. |
|
D M j G:i:s T Y |
|
H:m:s %m %i%s% %m%o%n%t%h |
|
H:i:s |
And here you can test your own flags if you should wish.
Differences and gotchas
You should note quite quickly that you use % to escape, not backslash (\). PHP gets to treat strings a bit different, and frankly, \\d\\u\\m\\b looks stupid (not to mention the quadruple escape, \\\\, if you want a single backslash in your output).
Another thing to note is that this code operates either directly on a Date object (which always have a time set), or a passed in value, which it interprets as a date. The script doesn’t help you in any way in setting a time. As a developer, you need to be careful when juggling dates on the client side, and also if you create any dates, don’t make assumptions regarding what timezone the runtime thinks it is.
I’ve obsoleted my old page, though it’s still on the site under a different name. It had some more rigerous testing (it’s using the old version of the script so take care), and also some stuff about localization which I don’t really think is that interesting anyway.
Tags: date, formatting, JavaScript

July 16th, 2008 at 12:16 am
Thanks Svend! Great script, really handy.
Rob
July 19th, 2008 at 8:36 am
Hi Svend! I am trying to get code to input the specific date wit todays date in the Java script: FSfncWriteFieldHTML(“DateForm”,”ArrDate”,”None”,100,”FutureShockDateSelector/images/FSdateSelector/”,”EN”,”true”,”true”, “19/07,2008″)
I am fairly new to Java. Do I just add your script in the head of the page and then just enter “d/m,Y” in the other script? Can’t be that easy is it?
Great site by the way.
July 19th, 2008 at 1:27 pm
FSfncWriteFieldHTML seems to be a product that creates a date selector control for your form. My little script merely formats dates, they are not related.
July 21st, 2008 at 4:19 pm
Thanks!
Very nice script!
November 19th, 2008 at 4:12 pm
Dômo svend. Script ha totemô î desu, yo!
December 6th, 2008 at 5:15 pm
[...] – bookmarked by 2 members originally found by staceybr on 2008-11-10 making "the nice" " Blog Archive " JavaScript date string formatting http://www.svendtofte.com/javascript/javascript-date-string-formatting/ – bookmarked by 4 members [...]
August 13th, 2009 at 11:52 pm
I guess this posting doesn’t even have a code sample! Well anyway, it’s supposed to be used ala:
dt.formatDate("F j, Y, g:i a");where
dtis your date object. Keep in mind that this code does not help you get a date into an object, it can only help format a date once you have it.March 5th, 2010 at 3:43 am
Hi, I came across this post while searching for help with JavaScript. I have recently switched browsers from Opera to IE. Just recently I seem to have a issue with loading JavaScript. Every time I go on a website that needs Javascript, my computer doesn’t load and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix it. Any aid is very appreciated! Thanks
March 11th, 2010 at 11:06 am
This script is just awesome, thank you very much!
May 25th, 2010 at 4:49 am
Hi, nice script.
And what if I want the string representing the date in UTC?
Should I dupplicate the switches and use the getUTCxxx variants or is there a simple solution?
Thanks
October 26th, 2011 at 11:33 am
Just wanted to say great function. It still amazes me that Javascript doesn’t natively have a date function similar to PHP. This is not a concern for me anymore! Once again, thanks :)
May 4th, 2012 at 3:46 am
Canada>Canada http://apopaequkif.de.tl edible solar models Shitty setting but that has to be one of the hottest woman(not to mention ass) that I have ever seen!
May 4th, 2012 at 8:53 pm
Cool site goodluck :) http://ajebyouly.de.tl canadian models I’m like the rest of you sad goons, I too have seen a lot of videos on this website. But those of you who moan saying that these Eros Exotica videos aren’t sleezy enough I happen to believe it’s their greatest strength. These Eros videos are very informative and make it less ‘pornographic’ and more sensual. I say Bravo.
May 6th, 2012 at 11:23 pm
Could I have an application form? http://agoohubamupi.de.tl little sister insest Joey,Loved to see you eat that cream pie.Wish i were there to lick that cum up for you.You guys get me hot!!! Thanks
May 7th, 2012 at 4:05 am
An estate agents http://eqahodyqyhu.de.tl little russian modles ok guy with british flag, he said shes anorexic because you can see her ribs pretty clearly and dont be racist
May 18th, 2012 at 2:17 am
joomla templates nulled…
[...]Everything old is new again » Blog Archive » JavaScript date string formatting[...]…
May 7th, 2013 at 3:39 am
Bonjour à tous,