By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

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

P: 3
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 :

Expand|Select|Wrap|Line Numbers
  1. var mydate=new Date()
  2. var theyear=mydate.getYear()
  3. if (theyear < 1000)
  4. theyear+=1900
  5. var theday=mydate.getDay()
  6. var themonth=mydate.getMonth()+1
  7. if (themonth<10)
  8. themonth="0"+themonth
  9. var theday=mydate.getDate()+1
  10. if (theday<10)
  11. theday="0"+theday
  12.  
  13. var displayfirst=theyear
  14. var displaysecond=themonth
  15. var displaythird=theday
  16.  
  17.  
  18. document.RequestForm.DOTS5.value=displayfirst+"-"+displaysecond+"-"+displaythird
  19. document.RequestForm.DOTF5.value=displayfirst+"-"+displaysecond+"-"+displaythird

Todays date is pulled from the server I guess then the "var theday=mydate.getDate()+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
Jan 31 '11 #1
Share this Question
Share on Google+
4 Replies


P: 16
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:
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2.  
  3.  var mydate = new Date(), theyear, themonth, theday, 
  4.      displayfirst, displaysecond, displaythird;
  5.  
  6.  mydate.setDate( mydate.getDate() + 1 );
  7.  
  8.  theyear = mydate.getFullYear();
  9.  
  10.  themonth = mydate.getMonth() + 1;
  11.  
  12.  if( themonth < 10 )
  13.   themonth = "0" + themonth;
  14.  
  15.  theday = mydate.getDate(); 
  16.  
  17.  if( theday < 10 )
  18.   theday = "0" + theday;
  19.  
  20.  displayfirst = theyear;
  21.  
  22.  displaysecond = themonth;
  23.  
  24.  displaythird = theday;
  25.  
  26.  document.forms[ 'RequestForm' ].DOTS5.value = displayfirst + "-" + displaysecond + "-" + displaythird;
  27.  
  28.  document.forms[ 'RequestForm' ].DOTF5.value = displayfirst + "-" + displaysecond + "-" + displaythird;
  29.  
  30. </script>
Feb 2 '11 #2

P: 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.
Feb 2 '11 #3

P: 16
@Sinclair
Here is the same code operating on a form:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2.    "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <title>Test Page</title>
  6. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  7. </head>
  8. <body>
  9. <form name = "RequestForm" action = "#">
  10. <p>
  11.  <input name = "DOTF5">
  12.  <input name = "DOTS5">
  13. </form>
  14. <script type='text/javascript'>
  15.  
  16.  var mydate = new Date(), theyear, themonth, theday, 
  17.      displayfirst, displaysecond, displaythird;
  18.  
  19.  mydate.setDate( mydate.getDate() + 1 );
  20.  
  21.  theyear = mydate.getFullYear();
  22.  
  23.  themonth = mydate.getMonth() + 1;
  24.  
  25.  if( themonth < 10 )
  26.   themonth = "0" + themonth;
  27.  
  28.  theday = mydate.getDate(); 
  29.  
  30.  if( theday < 10 )
  31.   theday = "0" + theday;
  32.  
  33.  displayfirst = theyear;
  34.  
  35.  displaysecond = themonth;
  36.  
  37.  displaythird = theday;
  38.  
  39.  document.forms[ 'RequestForm' ].DOTS5.value = displayfirst + "-" + displaysecond + "-" + displaythird;
  40.  
  41.  document.forms[ 'RequestForm' ].DOTF5.value = displayfirst + "-" + displaysecond + "-" + displaythird;
  42.  
  43. </script>
  44. </body>
  45. </html>
Feb 2 '11 #4

P: 3
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 :)
Feb 2 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.