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

Add days to a Date and display in a text field

P: 30
Hi,

I'm a novice. I need to add a certain number of days to a date entered in a field (dd/mm/yyyy) and then display the calculated date in another field in the same format, dd/mm/yyyy. Here is what I have so far. I appreciate your help:

Expand|Select|Wrap|Line Numbers
  1. function calDate() {
  2.  
  3. var date1 = new Date(document.getElementById('date_field1').value);
  4. document.getElementById("date_field2").value = date1.setDate(date1.getDate()+5);
  5.  
  6. }
  7.  
Many thanks.
Nov 27 '07 #1
Share this Question
Share on Google+
9 Replies


Expert 100+
P: 390
Expand|Select|Wrap|Line Numbers
  1. var myDate = new Date();
  2.  
  3. //add a day to the date
  4. myDate.setDate(myDate.getDate() + 1);
  5.  
  6. //add a week
  7. myDate.setDate(myDate.getDate() + 7);
  8.  
  9. //add a month
  10. myDate.setMonth(myDate.getMonth() + 1);
  11.  
  12. //add a year
  13. myDate.setYear(myDate.getYear() + 1);
Nov 27 '07 #2

P: 30
Hi,
Sorry, but I'm afraid this does not help me at all. Can anyhow help with respect to my specific example and detailed explanation I provided.
Nov 27 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Parse the date string using split(), then use setFullYear to set the date.

To set the second field, use the getDate(), getMonth() and getFullYear() methods.

See this reference.
Nov 27 '07 #4

P: 30
Could you please kindly provide a full example based on my original explanation. This would be very helpful and I would learn quicker.
Nov 27 '07 #5

acoder
Expert Mod 15k+
P: 16,027
Could you please kindly provide a full example based on my original explanation. This would be very helpful and I would learn quicker.
You should be able to do this yourself with the information, but anyway here you go:
Expand|Select|Wrap|Line Numbers
  1. function calDate() {
  2. var dateArr = document.getElementById('date_field1').value;
  3. var date1 = new Date();
  4. // assume entered in dd/mm/yyyy format - no validation check.
  5. date1.setFullYear(dateArr[2],dateArr[1]-1,dateArr[0]);
  6. date1.setDate(date1.getDate()+5);
  7. document.getElementById("date_field2").value = date1.getDate() + "/" + (date1.getMonth()+1) + "/" + date1.getFullYear();
The month value is between 0 and 11 (which is why you need to add and subtract 1 when getting and setting).
Nov 28 '07 #6

P: 30
Thank you very much! I did tried my best and spend quite a bit of time trying to work it out. As always you guys are absolutly terrific!!!! I tried the code provided, but I get "NaN/NaN/NaN" in the second date field. Is there a reason for that? I am entering a date in the format of dd/mm/yyyy in the first date field. Not sure where it's going wrong?
Nov 28 '07 #7

acoder
Expert Mod 15k+
P: 16,027
I tried the code provided, but I get "NaN/NaN/NaN" in the second date field. Is there a reason for that?
My fault for not testing. On line 5, you need to parse the strings using parseInt, e.g. parseInt(dateArr[2]), parseInt(dateArr[1])-1,...
Nov 29 '07 #8

P: 1
still not working with parseInt, getting "NaN/NaN/NaN"
Jan 19 '12 #9

100+
P: 116
Came across this thread so will complete the solution here for those still finding this in searches.

The problem is that the solution offered did not split the date entry to create the array used in the next line.

First line of the function should read
Expand|Select|Wrap|Line Numbers
  1. var dateArr = document.getElementById('date_field1').value.split('/');
Sep 26 '14 #10

Post your reply

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