SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to add 4 hours to current system time

    Hi
    all

    I want to validate the time field so that the user can enter the time more than 3 hours from the current time


    please help me out.


    Thanks
    MD.Samiuddin

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    JavaScript uses the time on your visitor's computer as the starting point so you don't want to use it for anything that really depends on any particular time being set.

    To get the time three hours from what their computer currently thinks the time is you would use:

    Code:
    var time = new Date();
    time.setHours(time.getHours()+3);
    alert('In three hours it will be ' +time.getHours() +':'+time.getMinutes()+':'+time.getSeconds());

    If you are going to be doing lots of date and time manipulations in JavaScript you might take a look at http://javascriptexample.net/dollarD.php

    The code to get the time three hours from now would then be:

    Code:
    var time = new $D();
    time.addHours(3);
    alert('In three hours it will be '+time.format('H:i:s'));
    Of course the extra code for the library isn't worth it unless you are actually going to be doing a lot of date and time processing and want to make it easier to read like this.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help and support.

    I am very grateful to you


    Thanks & Regards
    MD.Samiuddin


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
  •