Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simply splitting a date up?

    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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Only if you use strictly the Date() object:
    PHP Code:
    var today Date();
    var 
    thisDay today.split(' ')[0];
    var 
    thisMonth =  today.split(' ')[1]; 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nkightley
    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...

    PHP Code:
    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.

    PHP Code:
    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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •