473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 :

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
4 1966
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
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
@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
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

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

Similar topics

3
by: Chumley the Walrus | last post by:
I'm trying to use DatePart and Datediff functions to show the formatted m/dd/yyyy display for the dates of Monday and Friday of the current week. I want it to display as: Our Weekly Schedule...
1
by: laredotornado | last post by:
Hi, Does anyone know a predefined function (or can provide one) that takes three varaibles, $day, $month, and $year and returns whether or not all three constitute a valid date in which it is...
4
by: Christine | last post by:
I am having the strangest problem. I use the Date function in several of my forms and modules in an Access 2000 mdb. Lately, wherever in my code (in this one mdb) I use the Date function, it...
5
by: Paolo | last post by:
Hello, I have a field used to add date of births, named DOB. I would like to add another field, named it YOB, which automatically returns the year of birth from the previous field. Any help?...
4
by: laredotornado | last post by:
Hi, I am retrieving results from an SQL query, one of which is a date $arrival = $row; I would like to write a PHP function that formats the date in the following way: If the time part...
2
by: tthai | last post by:
Can someone help please I have 2 fields, date fied and period field, what I try to do is to automatically populate the period no field when entering a date field. Example: 1/1 - 28/1 - Period 1 ...
1
by: Quazzie | last post by:
I have a form in Access with 2 date fields for completion. I want to query data from the 1st of the month (e.g. 01 April, 2007) to yesterday. For yesterday I use '=Date()-1', which works great but...
5
by: bgrissom | last post by:
I just used the script that someone posted to change just the year in a date field and it worked great. I want to do the samething by just changing the month only thanks
1
by: naveenkongati | last post by:
Hi, How do we fetch only date and month from date column (Ex: 2008-11-10) in Mysql? Thanks
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.