SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display current time + 24 hrs

    For a sales page I want to display the time when the offer ends = in 24 hours of when the user is seeing the page or midnight the same day.

    I have this javascript code, but do not seem to have it right:

    Code:
    <script type="text/javascript"> 
    
    var currDate = new Date(); 
    var amount = 5; 
    var pad = (currDate) - parseInt(86400*amount);
    
    var newDate = new Date(pad); 
    document.writeln("But hurry - This offer expires " + newDate.toLocaleDateString()); 
     
    </script>
    Any idea what I am doing wrong? All this does it shows me today's date as the time.

    Christoph
    The-USA dot net - PR4 Web Directory

  2. #2
    SitePoint Zealot yacka's Avatar
    Join Date
    Aug 2006
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    //get milliseconds of current time plus extra day
    var pad = currDate.valueOf() + 86400000;

  3. #3
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did the trick.

    Thank you so much.

    Christoph
    The-USA dot net - PR4 Web Directory

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yacka that will not work correctly, as for Daylight Savings Time (DST) it will loose or gain an hour.

    Instead use

    Code:
    var date = new Date();
    var date_plus24hours = new Date(date.setDate(date.getDate()+1));
    
    // sometimes date.setDate(date.getDate()+1) has returned the milliseconds
    // since 1/1/1970 so you have to convert it to a date. Wrapping it in a date
    // makes sure that a date object is returned.



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
  •