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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts

    unable to swap year and day in date string.

    The string originally contains...

    26-07-2013

    and I am wanting to swap the year and day around to 2013-07-26 but the following is not doing it, what am I doing wrong?

    I split the script up more to have each part in a var first before joining them up with concat, but this still did not work.

    Code:
    fIDj1datesplit = fIDj1date.toString().split('-');
    			fIDj1datesplit2 = fIDj1datesplit[2];
    			fIDj1datesplit1 = fIDj1datesplit[1];
    			fIDj1datesplit0 = fIDj1datesplit[0];
    			fIDj1datetime = fIDj1datesplit2.concat(fIDj1datesplit1, fIDj1datesplit0, fIDj1hour, fIDj1min);

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    concat is for joining *arrays*.

    K.I.S.S.:
    Code:
    var t = fIDj1date.toString().split('-');
    fIDj1datetime = t[2] + "-" + t[1] + "-" + t[0];
    I do worry about your use of toString() there. It implies to me that fIDj1date is *not* a string, and so how can you be sure the toString() will indeed produce a string in the DD-MM-YYYY format?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    That's sort of what I had before, but it did not work. No errors show up in Web Console (Firefox)

    Code:
    fIDj1datesplit = fIDj1date.toString().split('-');
    fIDj1datetime = fIDj1datesplit[2] + fIDj1datesplit[1] + fIDj1datesplit[0] + fIDj1hour + fIDj1min; alert(fIDj1datesplit[2]);

    The alert show the var as undefined.

  • #4
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    without the toString() I get this error...

    TypeError: fIDj1date.split is not a function

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well start by doing
    Code:
    alert( fIDj1date.toString() );
    and tell us what *THAT* shows you. Exactly.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    [object HTMLInputElement]

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    ???? *WITH* the toString() there?

    Well, then, you are just not picking up the right thing, *at all*.

    Of course there is no such thing as fIDj1datesplit[2] because, when JavaScript tried to split "[object HTMLInputElement]" on the hyphens and finds ZERO hyphens, the resulting array has only element 0. Which contains, guess what, [object HTMLInputElement].

    So maybe you need to show us one heluva lot more code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    after placing many alerts around the code i find that i has a missing .value near the top of the code.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    So fIDj1date is a <form> field? In that case, indeed .value is needed and toString() is redundant: *ALL* .values of form fields are *ALWAYS* strings (and never null, by the by).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    var value='26-07-2013';

    value.split('-').reverse().join('-');

    /* returned value: (String)
    2013-07-26
    */

  • #11
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    Or with a regular expression
    Code:
    var value='26-07-2013';
    value=value.replace(/(\d\d)-(\d\d)-(\d\d\d\d)/,'$3-$2-$1');// => 2013-07-26


  •  

    Posting Permissions

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