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

    Change Time format

    Hi,

    I have a bit of a problem that no amount of Google searching has helped with! I have a database that returns time in the following format:

    hh:mm (i.e. no .ss/seconds)

    I want to be able to change it using JS to h:mm AM/PM. For example:

    21:30 would become 7:30 PM.

    Can anyone suggest any JS code that would help?

    Thanks,
    Neil

  • #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    See this wikipedia page.
    It could be not to difficult to make tests to subtract 12 (and add a.m. or p.m.) when necessary...

    Use a var hm='hh:mm'.split(':') to get the hours (hrs=parsInt(hm[0],10); to remove the leading zero) and minutes (mns=hm[1]).
    Then there is only to substract 12 if the hours are greater than 12, and to add (with a second test) a suffix ' a.m.' or ' p.m.' if the hours are less than 12.

    EDIT : Midnight are still particular cases. They can be treated by replacing the strings obtained (00:00 a.m.) by appropriate string (12:00 p.m.) with the replace method.
    Last edited by 007julien; 09-02-2013 at 03:33 PM.

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Sorry - I need a bit of hand-holding - v new to learning to write JS!

    I've got this so far - am I on the right track?

    Code:
    var myString = "[[time]]";
    var hm='hh:mm'.split(':')
    var hrs = parseInt(hm[0],10);
    var mns = parseInt(hm[1]);
    Thanks,
    Neil

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Is the database connection made
    by server side code ? Are you in
    control the server ? How does the
    time from the database get to the
    clients web page ?

  • #5
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaveyErwin View Post
    Is the database connection made
    by server side code ? Are you in
    control the server ? How does the
    time from the database get to the
    clients web page ?
    It all comes through PHP/MySQL as hh:mm, and the time field is a TEXT field. Therefore, I need to change the format on the client-side.

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Both MySQL and php have string
    functions that could do this on
    server side. How do you get the
    string containing the time into
    your javascript var ?

  • #7
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I'm afraid I don't fully understand the question.

    The problem I have is that the field is text, not time (due to a legacy issue going back a few years with the content management system). Therefore, the format that the time is stored is hh:mm, so I need to be able to change it using JS.

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Last edited by DaveyErwin; 09-02-2013 at 04:32 PM.

  • #9
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Sorry again - understand what you mean now - have just changed it to echo date("g:i a",strtotime($row["time"])). and it works perfectly - no need for JS


  •  

    Posting Permissions

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