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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2008
    Location
    Bangkok, Thailand
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Date rewrite .. (simple change of a string)

    hello NG .. i have a Date in a Var
    returned by a subfunction.
    like this:
    Code:
    return MochiKit.DateTime.toISODate(time).substr(2);

    the output is simply YY-MM-DD
    i would like to rewrite the date to be like DD.MM.YY

    but my code :
    Code:
    var day2 = MochiKit.DateTime.toISODate(time).substr(8);
    var month2 = MochiKit.DateTime.toISODate(time).substr(5).substring(3);
    var year2 = MochiKit.DateTime.toISODate(time).substr(2).substring(6);
    return day2 + '.' + month2 + '.' + year2;
    (sure somethings very stupid) does not work at all ;-)
    the output is always DD.DD.DD

    any ideas ??

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    var str='YY-MM-DD';
    var d1=str.substr(6);
    var m1=str.substr(3,2);
    var y1=str.substr(0,2);
    alert(d1+'.'+m1+'.'+y1);
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    oesxyl (03-23-2008)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    The syntax of the string.substr() method requires two arguments. The first is the start position of the substring and the second is the length of the substring to be returned.

    Code:
    yy ="08-03-23"
    var day2 = yy.substr(6,2);
    var month2 = yy.substr(3,2);
    var year2 = yy.substr(0,2)
    var date = day2 + '.' + month2 + '.' + year2;
    alert (date);
    abduraooft beat me to it!

  • Users who have thanked Philip M for this post:

    altf4 (03-23-2008)

  • #4
    New to the CF scene
    Join Date
    Mar 2008
    Location
    Bangkok, Thailand
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    works perfectly ...

    thanks alot .. well i still not sure what i did wrong .. but the output counts ;-)

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by Philip M View Post
    The syntax of the string.substr() method requires two arguments.
    I had the same in mind, but http://www.w3schools.com/jsref/jsref_substr.asp says length is optional.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    oesxyl (03-23-2008)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    If the length parameter is omitted, the substr() method extracts to the end of the string.

    So your

    var d1=str.substr(6);
    is the same as my
    var day2 = yy.substr(6,2);

    But for myself I would prefer to make the code explicit.

    Here's another method:-

    Code:
    <script type = "text/javascript">
    
    var date = "08/03/24"
    var sliced = date.split('/');
    //sliced[0] = sliced[0].substr(2,2);   // if desired to reduce 4-digit year to 2 digits
    var newdate = sliced[2] + ':' + sliced[1] + ':' + sliced[0];  // change order and separator to suit 
    alert (newdate);
    
    </script>
    Last edited by Philip M; 03-24-2008 at 07:49 PM. Reason: Added another method

  • Users who have thanked Philip M for this post:

    oesxyl (03-23-2008)


  •  

    Posting Permissions

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