SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple JS time manipulation

    I want a script to receive a time in the 24 hour time format HH:mm (e.g. 18:30) and remove 15 minutes from it.

    The problem is that if the time inputted was 00:00, then how do I get to 23:45?

    I'm sure it's easy if you're in the know.

    I can do this with PHP, but not with JS.

    I just need the function / basic line of code that does this. I can work out how to call it etc. myself.

    Thanks for any help.

    John

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function Time(time,less){
      var time=time.split(':'),date=new Date(2011,6,1,time[0]*1,time[1]*1+less),hrs=date.getHours(),min=date.getMinutes();
      return (hrs>9?hrs:'0'+hrs)+':'+(min>9?min:'0'+min);
    }
    
    alert(Time('4:24',-15));
    alert(Time('00:00',-15));
    /*]]>*/
    </script>
    </body>
    
    </html>

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect. Thanks Vic.


Bookmarks

Posting Permissions

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