1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript - getting the future time?

Discussion in 'Programming' started by mopacfan, Jan 14, 2005.

  1. #1
    I hope that made sense. I have a javascript function that takes the the current time and creates a random time in the future in order to refresh the page content. I was wondering if anyone here could tell me how to add the future time which is just a value of miliseconds, to the current time to figure out what the actual time will be when the page refreshes. If this does not make sense, mabey the code below will make it more clear:
    var rightnow = new Date();
    document.write("Time Now:" + rightnow.getHours() + ":" + rightnow.getMinutes());
    var randNum = Math.floor( Math.random() * 7200000) + 3600000;
    var newCheck = Math.round(randNum/60000)
    document.write(" - Next Check In:" + newCheck + " Minutes")
    setTimeout('x();', randNum)
    function x() {}
    Code (markup):

     
    mopacfan, Jan 14, 2005 IP
  2. Jackobo007

    Jackobo007 Peon

    Messages:
    195
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Did you mean this ?
    
    var rightnow = new Date();
    document.write("Time Now:" + rightnow.getHours() + ":" + rightnow.getMinutes());
    var randNum = Math.floor( Math.random() * 7200000) + 3600000;
    var newCheck = Math.round(randNum/60000)%60 + rightnow.getMinutes()
    var nexthour = rightnow.getHours() + Math.floor(Math.round(randNum/60000) / 60)
    document.write(" - Next Check In:" + nexthour + ":" + newCheck)
    setTimeout('x();', randNum)
    function x() {}
    
    Code (markup):
     
    Jackobo007, Jan 14, 2005 IP
  3. mopacfan

    mopacfan Peon

    Messages:
    3,273
    Likes Received:
    164
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey, that looks promising. I'll try it out. Thanks.
     
    mopacfan, Jan 14, 2005 IP
  4. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i think you mean this:

    I have also fixed a bug to show 02:48 or 11:02 instead of 2:48 and 11:2 as it did before

    
    var rightnow = new Date();
    
    var TimeM = rightnow.getMinutes();
    var TimeH = rightnow.getHours();
    
    var fTimeM = TimeM;
    var fTimeH = TimeH;
    
    if (TimeH < 10) TimeH = '0'+TimeH;
    if (TimeM < 10) TimeM = '0'+TimeM;
    
    document.write("Time Now:" + TimeH + ":" + TimeM);
    var randNum = Math.floor( Math.random() * 60000); //+ 3600000;
    var newCheck = Math.round(randNum/60000);
    
    fTimeM += newCheck-(Math.floor(newCheck/60))*60;
    if (fTimeM > 60)
    {
       x = Math.floor(fTimeM/60);
       fTimeH += x;
       fTimeM -= x*60;
    }
    
    fTimeH += Math.floor(newCheck/60);
    if (fTimeH >= 24)
    {
       fTimeH -=24;
    }
    
    if (fTimeH < 10) fTimeH = '0'+fTimeH;
    if (fTimeM < 10) fTimeM = '0'+fTimeM;
    
    document.write(" - Next Check In:" + newCheck + " Minutes");
    document.write(' - Next Check At:' + fTimeH + ' : '+fTimeM);
    setTimeout('x();', randNum);
    
    
    function x() 
    {
       document.location = document.location;
    }
    
    Code (markup):
     
    phrozen_ra, Jan 14, 2005 IP
  5. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Also I have made that refresh function for you....
     
    phrozen_ra, Jan 14, 2005 IP
  6. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It's safer and easier to work with the serial representation of a date when you need to do date math. For example:

    var now = new Date();
    var serial = now.valueOf();
    var then = new Date(serial + 5 * 60 * 1000);
    alert(then.toString());
    Code (markup):
    This will calculate a new date 5 minutes from now.

    J.D.
     
    J.D., Jan 14, 2005 IP