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 3 of 3

Thread: String Question

  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    198
    Thanks
    148
    Thanked 0 Times in 0 Posts

    String Question

    Hello,

    I need your help,

    I need to be able to extract both DateA and DateB from a string that will always read:

    Ex. 1 (format: dd/mm/yyyy)

    if (dateformat = dd-mm-yyyy) { reformat to: dd/mm/yyyy }
    ------------------------
    DateA-DateB
    20/06/2012-23/06/2012

    alert("DateA = 20/06/2012")
    alert("DateB = 23/06/2012")


    How can I parse these dates with either the and get their value?

    Much thanks and appreciation

    Jay

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,545
    Thanks
    89
    Thanked 4,934 Times in 4,895 Posts
    Clear as mud.

    You mean if somebody gives you
    Code:
    20-06-2012-23-06-2012
    how do you separate out the two dates?

    It's trivial if they give you
    Code:
    20/06/2012-23/06/2012
    You just do
    Code:
    var both = "20/06/2012-23/06/2012";
    var temp = both.split("-");
    var dateA = temp[0];
    var dateB = temp[1];
    But if all the delimiters are dashes, it's trickier. Is that what you are asking about?
    Be yourself. No one else is as qualified.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,545
    Thanks
    89
    Thanked 4,934 Times in 4,895 Posts
    One way: Treat ANY non digit character the same:
    Code:
    var both = "20/06-2012$23:06=2012";
    
    both = both.replace( /[^\d]/g,"-" ); // convert all non-digits to dash
    var temp = both.split("-");
    
    if ( temp.length != 6 ) { alert("kablooey! invald"); }
    
    var dateA = temp.slice(0,3).join("/");
    var dateB = temp.slice(3).join("/");
    Be yourself. No one else is as qualified.


  •  

    Posting Permissions

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