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 6 of 6
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Bangalore
    Posts
    75
    Thanks
    5
    Thanked 0 Times in 0 Posts

    How can I add hours to date ??

    Hi All,

    I am facing problem while adding 4 hours to date like : "25-9-2006 06:20:16"
    I have tried doing like this :

    var now = new Date("25-9-2006 06:20:16");
    var hrs = now.getHours();
    hrs += 4;
    var res=now.setHours(hrs);

    alert(now);

    it is giving me the result in UCT format, but I want the result in this "25-9-2006 10:20:16" format.Can anyone help me out ,its urgent ...

    thanx in advance ...

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,448
    Thanks
    11
    Thanked 598 Times in 578 Posts
    you can re-order and edit the "f(something)" lines of this:

    Code:
    
    Date.prototype.toShort= function () {
            function f(n) {
                return n < 10 ? "0" + n : n;
            }
    
          return ""+
    
    	f(this.getFullYear()) + "-" + 
    	f(this.getMonth() + 1) + "-" + 
    	f(this.getDate()) + " " + 
    	f(this.getHours()) + ":" + 
    	f(this.getMinutes()) + ":" + 
    	f(this.getSeconds()) +
    
    "";}//end Date.toShort
    
    //example
    var d=new Date;
    alert(d.toShort()) // "2009-05-21 01:01:39"
    Last edited by rnd me; 05-21-2009 at 08:04 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    New Coder
    Join Date
    May 2009
    Location
    Bangalore
    Posts
    75
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanx for the reply ...

    I am not getting how r u goining to call the function f and pass any value to this function ...

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,448
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Quote Originally Posted by Nirbhay View Post
    Thanx for the reply ...

    I am not getting how r u goining to call the function f and pass any value to this function ...
    you dont need to change or call anything really, i set it up so you could re-arrange the order of whole lines.


    you just use it right before you need it, the above code builds it in to all dates.

    the date syntax won't parse, but it's close:

    Code:
    var now = new Date("9/25/2006 06:20:16");
    var hrs = now.getHours();
    hrs += 4;
    var res=now.setHours(hrs);
    
    alert(now.toShort());
    EDIT:
    this might be a US-format only, not sure.
    browsers parse the Date.toLocale(""|Time|Date)String() methods' formats, which vary by regional settings.
    Last edited by rnd me; 05-21-2009 at 08:41 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,245
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Another way (easy to adjust for any number of days/hours/mins/secs ahead):-

    Code:
    <script type = "text/javascript">
    
    var z = 4; // number of hours ahead
    var zz = 30; // number of minutes ahead
    
    //var nd = new Date();  // time now
    
    var nd = new Date("9/25/2006 22:20:16");  // USA date format
    nd = nd.getTime() + (3600000 * z);  // + z hours
    nd = nd + (60000 * zz);  // + zz minutes
    var d = new Date(nd);
    var mm = d.getMonth() +1;
    if (mm <10) {mm = "0" + mm}
    var dd = d.getDate();
    if (dd <10) {dd = "0" + dd}
    var yy = d.getFullYear();
    var hrs = d.getHours();
    if (hrs <10) {hrs = "0" + hrs}
    var mns = d.getMinutes();
    if (mns <10) {mns = "0" + mns}
    var secs = d.getSeconds();
    if (secs <10) {secs = "0" + secs}
    futureDate = (dd + "-" + mm + "-" + yy + "  " + hrs + ":"  + mns + ":" + secs );  // UK date format
    alert (futureDate)
    
    </script>
    Last edited by Philip M; 05-21-2009 at 09:32 AM.

  • #6
    New Coder
    Join Date
    May 2009
    Location
    Bangalore
    Posts
    75
    Thanks
    5
    Thanked 0 Times in 0 Posts

    problem resolved

    thanx a lot ...


  •  

    Posting Permissions

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