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.

2 days ahead *code*

Discussion in 'JavaScript' started by sebastya, Jan 1, 2007.

  1. #1
    does anybody know how to show 2 days ahead of the current date?

    so today is 2nd january, 2007 .:. the script would present "4th january, 2007"

    I'd appreciate it alot. Thanks!
     
    sebastya, Jan 1, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Here's a working example:

    
    <html>
    <body>
    
    <script type="text/javascript">
    
    var d = new Date();
    document.write (d.toLocaleString())
    document.write ("<br/>");
    var n = 2; // Number of days
    d.setTime( d.getTime() + 1000 * n * 86400 );
    document.write (d.toLocaleString())
    
    </script>
    
    </body>
    </html>
    
    Code (markup):
    Some reputation would be nice :D
     
    ajsa52, Jan 2, 2007 IP
    sebastya likes this.
  3. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #3
    It's not advisable to use time functions to calculate dates.

    var now=new Date();
    
    alert(new Date(now.setDate(now.getDate()+2)))
    
    // Then format the properties of [I]now[/I] as required.
    
    Code (markup):
    Having said that, it's also inadvisable doing anything critical based on the client's system clock, which could be wrong.
     
    Logic Ali, Jan 2, 2007 IP
  4. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Why ?
    I've made many programs written in Unix/C using time() function (on server side of course), and has been working for years (even work before and after the 2000 effect).
     
    ajsa52, Jan 2, 2007 IP
  5. sebastya

    sebastya Well-Known Member

    Messages:
    2,449
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    138
    #5
    ajsa52: your code gave me both todays date and 2 days ahead,

    how do you make it to only show 2 days ahead?
     
    sebastya, Jan 2, 2007 IP
  6. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #6
    Here it is, using only Date() functions to avoid fear of "Logic Ali" ;) :

    
    <html>
    <body>
    
    <script type="text/javascript">
    d1 = new Date();
    var n = 2; // Number of days
    var d2 = new Date(d1.setDate(d1.getDate()+n));
    document.write (d2.toLocaleString())
    </script>
    
    </body>
    </html>
    
    Code (markup):
     
    ajsa52, Jan 2, 2007 IP
  7. sebastya

    sebastya Well-Known Member

    Messages:
    2,449
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    138
    #7
    thanks that code works fine :D is there a way u can remove the time?
     
    sebastya, Jan 3, 2007 IP
  8. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #8
    Replacing "toLocaleString" with "toLocaleDateString".
    You can find more info about Date functions Here
     
    ajsa52, Jan 3, 2007 IP
  9. sebastya

    sebastya Well-Known Member

    Messages:
    2,449
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    138
    #9
    thanks again :)
     
    sebastya, Jan 3, 2007 IP