How to fix function that gets tomorrows date? It fails on month end?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sinclair
    New Member
    • Jan 2011
    • 3

    #1

    How to fix function that gets tomorrows date? It fails on month end?

    Hi Guys, just signed up and hope someone may be able to help. Have a "Tomorrow" button on my website which pulls data from MySQL, this then displays a list of jobs loaded for the following day - works fine except on month ends.

    I should say that the site was built for me as I do not have a great understanding of Javascript etc but digging through the code currently being used is this :

    Code:
    var mydate=new Date()
    var theyear=mydate.getYear()
    if (theyear < 1000)
    theyear+=1900
    var theday=mydate.getDay()
    var themonth=mydate.getMonth()+1
    if (themonth<10)
    themonth="0"+themonth
    var theday=mydate.getDate()+1
    if (theday<10)
    theday="0"+theday
    
    var displayfirst=theyear
    var displaysecond=themonth
    var displaythird=theday
    
    
    document.RequestForm.DOTS5.value=displayfirst+"-"+displaysecond+"-"+displaythird
    document.RequestForm.DOTF5.value=displayfirst+"-"+displaysecond+"-"+displaythird

    Todays date is pulled from the server I guess then the "var theday=mydate.g etDate()+1" part is being used to add a day on. Clearly though this does not account for month ends, clicking the "tomorrow" button today the 31st Jan and the results try to display results for 32nd Jan rather than 1st Feb!!

    Sorry this is probably quite basic, I am hopeful I can just add a bit more code to account for the 12 dates per year that it causes an issue. Be grateful for any advice.

    Thanks

    Sinclair
    Last edited by Niheel; Jan 31 '11, 06:18 PM.
  • Logic Ali
    New Member
    • Jul 2010
    • 16

    #2
    In your script the date comes from the browser not the server.
    All that's required is to set tomorrow's date before reading any values:
    Code:
    <script type='text/javascript'>
    
     var mydate = new Date(), theyear, themonth, theday, 
         displayfirst, displaysecond, displaythird;
     
     mydate.setDate( mydate.getDate() + 1 );
     
     theyear = mydate.getFullYear();
         
     themonth = mydate.getMonth() + 1;
     
     if( themonth < 10 )
      themonth = "0" + themonth;
     
     theday = mydate.getDate(); 
       
     if( theday < 10 )
      theday = "0" + theday;
        
     displayfirst = theyear;
     
     displaysecond = themonth;
     
     displaythird = theday;
        
     document.forms[ 'RequestForm' ].DOTS5.value = displayfirst + "-" + displaysecond + "-" + displaythird;
     
     document.forms[ 'RequestForm' ].DOTF5.value = displayfirst + "-" + displaysecond + "-" + displaythird;
    
    </script>

    Comment

    • Sinclair
      New Member
      • Jan 2011
      • 3

      #3
      Thanks, you are right, date is on the footer of the page and it is being pulled from that, date on page ultimately comes from the date set on the pc not server, indeed it seems if I change date manually on my system it correspondingly changes for the webpage also which confirms it.

      Sadly having replaced with your code above and resetting date to 31 Jan, it is still trying to show results for 32 Jan so still stumped, cheers for trying though.

      Comment

      • Logic Ali
        New Member
        • Jul 2010
        • 16

        #4
        Originally posted by Sinclair
        Sadly having replaced with your code above and resetting date to 31 Jan, it is still trying to show results for 32 Jan so still stumped, cheers for trying though.
        Here is the same code operating on a form:
        Code:
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
           "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
        <title>Test Page</title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <form name = "RequestForm" action = "#">
        <p>
         <input name = "DOTF5">
         <input name = "DOTS5">
        </form>
        <script type='text/javascript'>
        
         var mydate = new Date(), theyear, themonth, theday, 
             displayfirst, displaysecond, displaythird;
         
         mydate.setDate( mydate.getDate() + 1 );
         
         theyear = mydate.getFullYear();
             
         themonth = mydate.getMonth() + 1;
         
         if( themonth < 10 )
          themonth = "0" + themonth;
         
         theday = mydate.getDate(); 
           
         if( theday < 10 )
          theday = "0" + theday;
            
         displayfirst = theyear;
         
         displaysecond = themonth;
         
         displaythird = theday;
            
         document.forms[ 'RequestForm' ].DOTS5.value = displayfirst + "-" + displaysecond + "-" + displaythird;
         
         document.forms[ 'RequestForm' ].DOTF5.value = displayfirst + "-" + displaysecond + "-" + displaythird;
        
        </script>
        </body>
        </html>

        Comment

        • Sinclair
          New Member
          • Jan 2011
          • 3

          #5
          Brilliant stuff, now working. Think it was a simple cache issue earlier and was in fact still loading old script.

          Many thanks for the help :)

          Comment

          Working...