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

    DateTime format conversion

    Hi All,

    I want to convert my date time format from 2012-07-23 15:50:56
    this format to 23 July 2012, 3:30 PM



    how can I do that using javascript functions?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Where does your existing time format come from?

    But this topic has been covered very recently in this forum. Have a look at Javascript Time

    Customer in UK travel agent shop: - "What month is it now in Australia?"
    Last edited by Philip M; 07-24-2012 at 03:22 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
    New Coder
    Join Date
    Jul 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want to convert my date time format from 2012-07-23 15:50:56
    this format to 23 July 2012, 3:30 PM

    Any straightway functions available

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by michael_1980 View Post
    I want to convert my date time format from 2012-07-23 15:50:56
    this format to 23 July 2012, 3:30 PM

    Any straightway functions available
    It is pointlesss to just repeat yourself. I'll ask you one more time. Where does your existing date format come from? Is it a string value?
    Assuming that your date is a string value,

    Code:
    <script type = "text/javascript">
    
    var datestring = "2012-07-23 15:50:56";
    var mths = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    var ampm = "AM"
    datestring = datestring.replace(/[^\d]/gi,"~");  // replace everything not a digit by a ~
    var dsplit = datestring.split("~");  // and split the string at ~ into an array
    var dt = Number(dsplit[2]);
    var mth = dsplit[1]-1 // months in Javascript are 0-11
    var yr = dsplit[0];
    var hrs = Number(dsplit[3]);
    if (hrs > 11) {ampm = "PM"}
    if (hrs > 12) {hrs = hrs -12}
    var mins = Number(dsplit[4]);
    if (mins <10) {mins = "0" + mins}  
    var newdate = dt + " " + mths[mth] + " " + yr + ", " + hrs + ":" + mins + " " +  ampm;
    
    alert (newdate);
    
    </script>
    Last edited by Philip M; 07-25-2012 at 11:47 AM.

    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
    •