Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>Simply splitting a date up?

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).


 

TOP

For more info

setting a minimum size
Auto Preview of Image 
Refresh with a query s
JS Validation obsolete
Javascript Pop up func
Slide show transitions
I'm a newbie...please!
how to write coding in
Advanced Debug Info 
.js file doesn't work 

News Archive

Adding Graphic To Text
re-direct form after s
pop up window controls
Clock on homepage - He
passing a param from a
Password Procection wi
document.title in same
focus onload 
Counting Wrong from Ri
Help needed on how to 

Related stories:

validate using ONSUBMIT vs ONUNLOAD
data from forms
Cross-Browser Menu Now Working, BUT Now Have FireFox Refresh Loop
How do you write maintainable code?
How to excecute 2 different form validation Java scrpt with 1 submit button ?
I want to Resize frames on a button click
Need Cross-Browser Capable Menu / Script - event.srcElement to getElementById
Link Extractor Mod to Get Page Domains

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics