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
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    39
    Thanks
    4
    Thanked 0 Times in 0 Posts

    My substring() isn't working

    What am not getting about substring()?
    I'm trying to separate out dd and yy
    from input coming in as mm/dd/yy.
    (My mm is parsing out correctly).
    Code:
    var mm = mydate.substring(0, mydate.indexOf("/"));
    var ddlength = (mydate.lastIndexOf("/")-1-mydate.indexOf("/") );
    var ddstartat = (mydate.indexOf("/")+1);
    var dd = mydate.substring(ddstartat, ddlength);
    var yylength = (mydate.length - 1 - mydate.lastIndexOf("/"));
    var yystartat = (mydate.lastIndexOf("/")+1);
    var yy = mydate.substring(yystartat, yylength);

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Change substring to substr. These are two different methods.

    Code:
    <script type = "text/javascript">
    var mydate = "12/23/08"
    var mm = mydate.substr(0, mydate.indexOf("/"));
    var ddlength = (mydate.lastIndexOf("/")-1-mydate.indexOf("/") );
    var ddstartat = (mydate.indexOf("/")+1);
    var dd = mydate.substr(ddstartat, ddlength);
    var yylength = (mydate.length - 1 - mydate.lastIndexOf("/"));
    var yystartat = (mydate.lastIndexOf("/")+1);
    var yy = mydate.substr(yystartat, yylength);
    
    alert (mm + "/" + dd + "/" + yy)
    
    </script>
    The syntax is substr(x,y) where x is the start position of the substring and y is the length of the substring to be returned.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 08-14-2008 at 07:49 PM. Reason: Code tags added

  • Users who have thanked Philip M for this post:

    ruffy (08-14-2008)

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    39
    Thanks
    4
    Thanked 0 Times in 0 Posts
    BINGO Philip M!! Thank you my friend.


  •  

    Posting Permissions

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