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

    Sorting Date & Time

    Hi All,

    I've done a few searches on it, but I can't seem to find the correct order for my date / time script.

    I have a field that returns the date/time in the following format:

    Y-m-d H:i:s (i.e. yyyy-mm-dd hh:mm:ss)

    What I want to do is rearrange this so that it says:

    'dd' 'month-name' 'yyyy' at 'time'

    Anyone have any ideas?

    Thanks,
    Neil

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Here you are:-

    Code:
    <script type = "text/javascript">
    
    var field = "2011-06-25 18:02:20";  // the field as supplied 25th June 2011
    var months=['January','February','March','April','May','June','July','August','September','October','November','December']
    var yr = parseInt(field.substr(0,4),10);
    var mn = parseInt(field.substr(5,2),10);  // month to decimal
    var dt = parseInt(field.substr(8,2),10);
    var tim = field.substr(11,16);
    var newfield = dt + " " + months[mn-1] + " " + yr + " at " + tim;  // months in Javascript are 0-11
    alert (newfield);
    
    </script>
    An alternative:-

    Code:
    <script type = "text/javascript">
    
    var field = "2011-06-25 18:02:20";  // the field as supplied
    var months=['January','February','March','April','May','June','July','August','September','October','November','December']
    var d = field.split("-");
    d[1] = parseInt(d[1],10); // month to decimal
    var ds = d[2].split(" "); // split off time
    var newfield = ds[0] + " " + months[d[1]-1] + " " + d[0] + " at " + ds[1];
    alert (newfield);
    
    </script>
    Quizmaster: In the early fifteenth century Owain Glyndwr led a rebellion against the ruling English in which country?
    Contestant: China.
    Last edited by Philip M; 06-25-2011 at 10:03 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Hi Philip M,

    Thanks for this - it sorts the date into the correct format, but for some reason does not display the data on the webpage, but rather I get a popup 'message from web-page' with the date.

    I've looked at the code but I can't work out how or why this is happening?

    Thanks,
    Neil

  • #4
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Sorry - scap that - changed 'alert' to 'document.write'. Works perfectly now - many thanks indeed.

    Neil

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    Use DOM methods to display the information.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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