...

View Full Version : Simply splitting a date up?



nkightley
10-12-2005, 01:23 PM
This is really starting to annoy me now!

Can anyone tell me how to simply split a date object up so you can extract the day in DDD format & the month in MMM format WITHOUT having to set up an array?

alert(new Date()) returns 'Wed Oct 12 13:22:01 UTC+0100 2005'

so you must be able to strip the 'Wed' & 'Oct' out of there?

Cheers

Neil

Vladdy
10-12-2005, 01:33 PM
RT (http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Predefined_Core_Objects:Date_Object)FM (http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Date)
:thumbsup:

Kor
10-12-2005, 03:55 PM
Only if you use strictly the Date() object:


var today = Date();
var thisDay = today.split(' ')[0];
var thisMonth = today.split(' ')[1];

ca_redwards
10-12-2005, 05:46 PM
This is really starting to annoy me now!

Can anyone tell me how to simply split a date object up so you can extract the day in DDD format & the month in MMM format WITHOUT having to set up an array?

alert(new Date()) returns 'Wed Oct 12 13:22:01 UTC+0100 2005'

so you must be able to strip the 'Wed' & 'Oct' out of there?

Cheers

Neil

Sounds like a school assignment... but you can chop it up by whitespace...


var today=new Date();
var token=today.toString().split(/\s+/g);
DDD=token[0];
MMM=token[1];


In practice, I use arrays (built from strings), since those are much easier to recognize later.


var today=new Date();
DDD="Sun Mon Tue Wed Thu Fri Sat".split(" ")[today.getDay()];
MMM="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" ")[today.getMonth()];


For code readability, I've made some interesting observations...

Writing code comments is unnatural. Coding is like doing math, but writing code comments is more like English composition. It's really hard to do both well at the same time.
Later on, string literals in code can be almost as illuminating as actual code comments.
Nested parentheses are confusing. I'll often write an expression (using heavily nested parentheses) however it first comes into my head, then refactor it afterward (to re-render nested subexpressions as simple local variables).
Always type both matching braces before entering any content. It takes only two more keystrokes (the left arrow after the closing brace, then the right arrow after the content), but this habit prevents syntax errors arising from mismatched curly braces, parentheses, square brackets, single quotes, double quotes, and angle brackets (for html tags).


I'd ask the other readers what habits they've adopted to make their code more readable, but I'm afraid that the moderator would move this entire post to its own thread (thus depriving you of my offered solution).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum