|
Comments and Discussions
|
 |
 |
after that i have replaced with the string MMM now its working fine
|
|
|
|
 |
Most helpful utility for javascript date !!!
|
|
|
|
 |
Nice one, still the download is missing and z prototype is not mentioned nowhere.
Cheers
|
|
|
|
 |
Thanks for pointing that out.
The 'z' prototype is in another article, Extending JavaScript Intrinsic Objects with Prototypes[^], I used zp for 'zero padding' on that one. To download just copy the prototype and the name arrays and paste them in a new .js file.
There is no spoon.
|
|
|
|
 |
It was not difficult to make it work, however, there is an agreed standard on CP, each article that shows some code should also have the download for that code, if possible in an working example.
Beside this details, neat job!
Cheers
“The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser men so full of doubts.”
|
|
|
|
 |
Good to know, thanks!
There is no spoon.
|
|
|
|
 |
Hi,
Thanks so much for this nifty script. Putting all the suggestions together and changing s/p to x (s/p conflicts with the other options so I just picked x since it was free - is there another commonly used one?)
<script type="text/javascript"> var gsMonthNames = new Array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
var gsDayNames = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' );
String.prototype.zf = function(l) { return '0'.string(l - this.length) + this; } String.prototype.string = function(l) { var s = '', i = 0; while (i++ < l) { s += this; } return s; } Number.prototype.zf = function(l) { return this.toString().zf(l); }
// the date format prototype Date.prototype.format = function(f) { if (!this.valueOf()) return 'n.a.';//
var d = this; return f.replace(/(yyyy|yy|y|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|t|x)/gi, function($1) { switch ($1) { case 'yyyy': return d.getFullYear(); case 'yy': return (d.getFullYear()%100).zf(2); case 'y': return (d.getFullYear()%100); case 'MMMM': return gsMonthNames[d.getMonth()]; case 'MMM': return gsMonthNames[d.getMonth()].substr(0, 3); case 'MM': return (d.getMonth() + 1).zf(2); case 'M': return (d.getMonth() + 1); case 'dddd': return gsDayNames[d.getDay()]; case 'ddd': return gsDayNames[d.getDay()].substr(0, 3); case 'dd': return d.getDate().zf(2); case 'd': return d.getDate(); case 'HH': return d.getHours().zf(2); case 'H': return d.getHours(); case 'hh': return ((h = d.getHours() % 12) ? h : 12).zf(2); case 'h': return ((h = d.getHours() % 12) ? h : 12); case 'mm': return d.getMinutes().zf(2); case 'm': return d.getMinutes(); case 'ss': return d.getSeconds().zf(2); case 's': return d.getSeconds(); case 't': return d.getHours() < 12 ? 'am' : 'pm'; case 'x': var i = d.getDate() % 10; return 'thstndrd'.substr(2 * (i < 4) * i, 2); } } ); } </script>
Murray
|
|
|
|
 |
Whoops, I left the wrong bit in from my testing:
case 'x': var i = d.getDate() % 10; return 'thstndrd'.substr(2 * (i < 4) * i, 2);
should be:
case 'x': switch (d.getDate()) { case 1: case 21: case 31: return "st"; case 2: case 22: return "nd"; case 3: case 23: return "rd"; default: return "th"; }
Sorry 'bout dat
Murray
|
|
|
|
 |
Another commonly used date parts would be the time zone specifier (GMT, CMT, etc.) and the '07 year format. Please note that the 'x' case may yield 12nd for 12. Be careful with that, try another approach for ordinals...
There is no spoon.
|
|
|
|
 |
I am using:
case 'x':
switch (d.getDate())
{
case 1:
case 21:
case 31:
return "st";
case 2:
case 22:
return "nd";
case 3:
case 23:
return "rd";
default:
return "th";
}
which seems to work properly for all the numbers I have tested (including 12).
|
|
|
|
 |
Jai! ai am javin som trobel loguin in to mai jotmeil acaunt. it sims dat mai pasbuord is not ricognaisd, iven doug ai meid shur dat ol de lirel lait bulbs on mai kibord ar torned on (including the guan coled caps loc)
ai guonder if llu can jelp mi bicos llu jav writen so meni artikels so ai gues llu jav jotmeil tu....
it guas very jard tu faind llu, BUT I DID SO I AM F***ING AWESOME!!! THE MINE SO BIG SO THE MINE SO FINE SO TOR2K QUE ARRECHO EL CD QUE ME MANDASTE!!! DIOS TE BENDIGA HIJO MIOOOOOOOOO! SO BIGGGGGGG!!! D.A.R.E. JAJAJAJA A3VT! LOVIN YA INZA! U CAN RUN BUT YOU CANT HIDE! JAJAJA,
there is no spoon.
PD. FREEEEENAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!
|
|
|
|
 |
what up d mine!!!! hacker mardito... me alegro q t gustara el CD... disfrutenlo! i love u 2!
Try not to bend the spoon, that's impossible; instead try to realize the truth: There is no spoon.
|
|
|
|
|
 |
Great article, though it would be nice if you added the Zero-Fill functions to your article and not only in the comments
Anyway; just a little update if you'd like to use C#-like formating;
return f.replace(/(yyyy|yy|y|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|t)/gi,
function($1)
{
switch ($1)
{
case 'yyyy': return d.getFullYear();
case 'yy': return (d.getFullYear()%100).zf(2);
case 'y': return (d.getFullYear()%100);
case 'MMMM': return gsMonthNames[d.getMonth()];
case 'MMM': return gsMonthNames[d.getMonth()].substr(0, 3);
case 'MM': return (d.getMonth() + 1).zf(2);
case 'M': return (d.getMonth() + 1);
case 'dddd': return gsDayNames[d.getDay()];
case 'ddd': return gsDayNames[d.getDay()].substr(0, 3);
case 'dd': return d.getDate().zf(2);
case 'd': return d.getDate();
case 'HH': return d.getHours().zf(2);
case 'H': return d.getHours();
case 'hh': return ((h = d.getHours() % 12) ? h : 12).zf(2);
case 'h': return ((h = d.getHours() % 12) ? h : 12);
case 'mm': return d.getMinutes().zf(2);
case 'm': return d.getMinutes();
case 'ss': return d.getSeconds().zf(2);
case 's': return d.getSeconds();
case 't': return d.getHours() < 12 ? 'A.M.' : 'P.M.';
}
}
);
-------------------
Kjetil Klaussen
-------------------
|
|
|
|
 |
The prototype of 'zf' would be appriciated
|
|
|
|
 |
Ok, here it goes:
// Zero-Fill
String.prototype.zf = function(l) { return '0'.string(l - this.length) + this; }
As you can see, it depends on the string prototype, an VB-like string concatenator:
// VB-like string
String.prototype.string = function(l) { var s = '', i = 0; while (i++ < l) { s += this; } return s; }
Just bear in mind there's no check for the l (length) parameter, so you must always provide a number, and finally, you must create a number prototype for it (kind of an override) in order to use it directly on numbers:
Number.prototype.zf = function(l) { return this.toString().zf(l); }
I hope this helps.
|
|
|
|
 |
Since you're using a case-insensitive regular expression, you should ensure that the switch statement is also case-insensitive. For example, a format string of YYYY will not return anything, since it won't match any of the case labels.
You should also specify each valid option within the expression, rather than relying on two or more matches of any characters from a specified group. For example, try using the format string yyyymmdd , which will return nothing.
return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/gi,
function($1)
{
switch ($1.toLowerCase())
{
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
 |
hmm... thanks. you're right, and your suggestions work.
"There is a strength in the union even of very sorry men." - Homer
|
|
|
|
 |
Some may find this small addition useful:
return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p|sup)/gi,
This adds a new case, "sup", for a superscript on the date:
case 'sup':
switch (d.getDate())
{
case 1:
case 21:
case 31:
return "st";
case 2:
case 22:
return "nd";
case 3:
case 23:
return "rd";
default:
return "th";
}
}
Thanks for this extremely useful function!
|
|
|
|
 |
Hey what a great idea. You can also group all the cases with a mod operation:
case 'sup':
switch (d.getDate() % 10)
{
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
Be careful when using format specifiers, though; "sup" may occur in the format string in a phrase; that's why I came up with "a/p" i.o. simply "ap".
Thanks, I'm glad it helped you!
|
|
|
|
 |
No you can't - instead of 11th, 12th and 13th, your code would return 11st, 12nd and 13rd!
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
 |
Too bad you're right. Ok, so I guess a switch is in order.
|
|
|
|
 |
Why not just use this?
switch ( ( d.getDate() % 100 > 3 && d.getDate() % 100 < 21 ) ? 0 : d.getDate() % 10)
|
|
|
|
 |
If it works, that's fine!
I would save some d.getDate() calls by setting up a variable, though. Not that users will notice, but programmers should!
There is no spoon.
|
|
|
|
 |
Ok, I changed the 'sup' format specifier with 's/p' and replaced the switch with a math operation:
case 's/p': var i = d.getDate() % 10; return 'thstndrd'.substr(2 * (i < 4) * i, 2);
You see, i (date mod 10) ranges from 0 to 9, but only 1, 2 and 3 have specific ordinal suffixes, so with a little math, and in a fashion similar to a choose statement in VB, I got rid of the switch statement for (hopefully) faster execution.
The math goes as follows: (i < 4) yields true for 0, 1, 2, 3 and false for everything else; fortunately for us, booleans yield 1 or 0 when used in arithmetic operations (in VB, 'true' actually equals -1). So this simple expression filters out the 'uninteresting' cases.
Next, we need the actual i value to pinpoint each case, that's why we multiply. This is also why we calculate and store i ahead of time, for we'll use it twice in the expression.
The rest involves doubling the value to get a 2-char index into the strange string we sub into, which is nothing but the ordinal suffixes, th st nd rd.
Incidentally, I realized you would need another date specifier to avoid presenting '06th', that is, skip the zero fill. But then again this datepart specifier itself might be enough (or maybe a 5d, ddddd that is) if you simply prepend the date.
Again, I just had fun figuring out an alternative to the switch. Richard Deeming corrected this anyway by pointing out that we'll get 11st i.o. 11th, 12nd and 13rd which are totally wrong; I guess the switch is in order, or an even messier math op.
|
|
|
|
|
 |
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
|