473,387 Members | 1,812 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,387 software developers and data experts.

Add days to a Date and display in a text field

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
9 11388
mzmishra
390 Expert 256MB
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
cptuser
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
16,027 Expert Mod 8TB
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
cptuser
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
16,027 Expert Mod 8TB
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
cptuser
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
16,027 Expert Mod 8TB
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
13564
1
still not working with parseInt, getting "NaN/NaN/NaN"
Jan 19 '12 #9
robertybob
116 64KB
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

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

Similar topics

1
by: John Davis | last post by:
I want to display credit card fields when the user selects the option paid membership. If user selects the option free membership, then the credit card text field will disappear. Here's the code I...
3
by: Leo Muller | last post by:
Hi, Very often I have a problem with date formats. There are cases I have to cast the date to text and from text to date on a different server. However, sometimes the date formats on the servers...
1
by: David | last post by:
Hi, I have a continuous form. For each record I have a field 'HeldDate' (Text Field from a table) Against each record I have a button which sets the visibility of this text box to 'True' and...
7
by: Benton | last post by:
Hi there, I have a text box which will receive its value from a pop-up date picker. The user should not be able to edit this field with the keyboard or mouse. I am using ASP.NET. If I set the...
3
by: jim.murphy | last post by:
I am not sure If I can do this with a lookup, but what I would like to do is perhaps use lookup to retrieve a control date from an unassociated table to control what date is entered in another...
8
by: libsfan01 | last post by:
how can i use regular expressions to ensure a mysql format date entry in a text field? thanks marc
10
by: dkyadav80 | last post by:
<html> /// here what shoud be java script for: ->when script run then not display all input text field only display selection field. ->when user select other value for institute only this...
7
by: kirkgilbert | last post by:
I am trying to do an onchange event in a form using a text field. The form is tied to a record set that is part of a repeated region. One the first record when I edit the data it works perfectly. ...
1
DebadattaMishra
by: DebadattaMishra | last post by:
Introduction In case of rich applications, you must have observed that a text field behaves like a dynamic combo box. When the user enters some characters in the text field, a popup will come up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...

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.