www.webdeveloper.com
Results 1 to 5 of 5

Thread: add 180 seconds to a date

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    14

    add 180 seconds to a date

    Hi every one
    I've a date in a variable like this :
    var d="2013-05-11 14:48:46"
    I want to add 180 seconds to it via javascript and get the new date . sometimes it's much more than 180 seconds , maybe 2000 second
    How can I do that ?
    I've tried several ways but non of them worked .

    Thanks

  2. #2
    Join Date
    Apr 2009
    Posts
    14
    anyidea ?

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Use the date object with something like this :
    Code:
    <script type="text/javascript">
    
    var d="2013-05-11 14:48:46";
    // Create a date object with year, javascript month (from 0 to 11), date, hour, min, sec
    var dte=new Date(d.substr(0,4),d.substr(5,2)-1,d.substr(8,2),d.substr(11,2),d.substr(14,2),d.substr(17,2));
    // To add
    var secondsToAdd=180;
    // Create a second object with the same value of (milliseconds) plus the gap in milliseconds
    var ndt=new Date(dte.valueOf()+secondsToAdd*1000);
    // See the results :
    alert('Given date : '+dte.toLocaleString()+'\n add : '+secondToAdd+' seconds\nResult : '+ndt.toLocaleString())
     
    </script>
    Last edited by 007Julien; 05-11-2013 at 10:47 AM.

  4. #4
    Join Date
    Sep 2007
    Posts
    301
    I try
    var dte = new Date( d.replace(/-/g,' ') );

    Code:
      
    <script type="text/javascript">
    // http://www.w3schools.com/js/js_obj_date.asp
    
    var d="2013-05-11 14:48:46";
    // Create a date object with year, javascript month (from 0 to 11), date, hour, min, sec
    //var dte=new Date(d.substr(0,4),d.substr(5,2)-1,d.substr(8,2),d.substr(11,2),d.substr(14,2),d.substr(17,2));
    //alert(dte.getMonth()); // 4
    var s = d.replace(/-/g,' '); 
    // alert('s= '+s);
    
    var dte = new Date(s); 
    // alert(dte.valueOf() );
    //alert(dte.getMonth()); // 4
    //alert('dte=  '+dte);
    // To add
    var secondsToAdd=180;
    // Create a second object with the same value of (milliseconds) plus the gap in milliseconds
    var ndt=new Date(dte.valueOf()+secondsToAdd*1000);
    // See the results :
    alert('Given date : '+dte.toLocaleString()+'\n add : '+secondsToAdd+' seconds\nResult : '+ndt.toLocaleString())
     
    </script>
    İyiliği emret. Kötülükten alıkoy. (31/16)

    hasbunallahuVeNimelVekil++

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Ayse : You are rigth, but the dateString format seems to depend on the country...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles