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
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Formatting date and time

    Hi All,

    I wonder if you can help me with some time and date formatting in JS.

    I already use a neat date formatting JS script which is useful when a PERL script returns a date field of 'DATE' by converting it from yyyy-mm-dd to dd month(in text) yyyy:

    <script type = "text/javascript">

    var months = ["January","February","March","April","May","June","July","August","September","October","November"," December"];
    var myString = "[[date]]";
    var mySplitResult = myString.split("-");
    var mm = parseInt(mySplitResult[1],10)-1; // note that months are 0-11
    document.write(mySplitResult[2] + " " + months[mm] + " " + mySplitResult[0] );

    </script>


    I have, however, run across a little problem where I have results coming through with date AND time, e.g.

    18:12 2010-07-14

    What I need is something that will reorder the above to:

    14 July 2010 at 18:12

    Can someone help please!

    Regards,
    Neil

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,657 Times in 4,619 Posts
    Utterly untested. Off the top of my head:
    Code:
    <script type = "text/javascript">
    
    var months = ["January","February","March","April","May","June","July","August","September","October","November"," December"];
    var myString = "[[date]]";
    var temp = myString.split(" ");
    var theTime = temp[0];
    var theDate = temp[1];
    var mySplitResult = theDate.split("-");
    var mm = parseInt(mySplitResult[1],10)-1; // note that months are 0-11
    document.write(mySplitResult[2] + " " + months[mm] + " " + mySplitResult[0] 
          + " " + theTime);
    </script>
    That is *assuming* that the only space in the [[date]] will be the one between the time and the date.
    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.

  • Users who have thanked Old Pedant for this post:

    howard-moore (07-15-2010)

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Excellent - works perfectly - thank you very much.


  •  

    Posting Permissions

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