Populating Date Field using Form attribute
Robert Lanzone created the topic: Populating Date Field using Form attribute
Hi,
I have a two field form.
I am trying to subtract 180 days from a field and then populating it in another date field
This is my javascrtipt:
function start_date()
{
var d = new Date(document.getElementById('wdform_4_element25').value);
return new Date(Date.parse(d) - (180 * 86400 * 1000)).toDateString();
}
wdform_4_element25 is the Start Date that a user will input a date.
wdform_5_element25 is the date filed I want to subtract 180 days from the date put in wdform_4_element25
I have the attribute for wdform_5_element25 as Name: onclick, Value: start_date();
Nothing populates when I click into the field for wdform_5_element25 after a date is placed in wdform_4_element25 .
Please Log in or Create an account to join the conversation.
- Topic Author
- Robert Lanzone
- Offline
- Fresh Boarder
- Posts: 12
- Thank you received: 1
9 years 4 months ago
Support replied the topic: Populating Date Field using Form attribute
Dear Robert,
Could you please give us the link to your form, so we can check the JS errors on the form page?
Thank you.
Best regards,
Web-Dorado team
Joomla extensions ,
Wordpress plugins ,
Drupal modules
Please Log in or Create an account to join the conversation.
9 years 4 months ago
Robert Lanzone replied the topic: Populating Date Field using Form attribute
Here is the link:
www.traveltipsandpicks.com/my-profile/test-form
Please Log in or Create an account to join the conversation.
- Topic Author
- Robert Lanzone
- Offline
- Fresh Boarder
- Posts: 12
- Thank you received: 1
9 years 4 months ago
Support replied the topic: Populating Date Field using Form attribute
Dear Robert,
Please give us (to [email protected]) temporary admin access to your website. We will take a look at the Javascript and try to find a solution for the problem as soon as possible.
Also please don't forget to mention the full link of your forum post in the e-mail.
Thank you.
Best regards,
Web-Dorado team
Joomla extensions ,
Wordpress plugins ,
Drupal modules
Please Log in or Create an account to join the conversation.
9 years 4 months ago
Robert Lanzone replied the topic: Populating Date Field using Form attribute
Thanks for the example. I refined it to the following:
I included date.format.js in my template header. This code can be found at
github.com/jacwright/date.format/blob/master/date.format.js
In template header:
<script src="/templates/.../date.format.js" type="text/javascript"></script>
I have two date fields in my form
Arrival Date (wdform_4_element29)
Departure Date (wdform_5_element29)
My form JavaScript has the following function:
function change_date()
{
var d = new Date(document.getElementById('wdform_4_element29').value);
d.setTime(d.getTime() + 86400000);
var n = d.format("D, M d, Y");
wdformjQuery('#wdform_5_element29').val(n);
}
The "Arrival Date" has an additional attribute Name - onchamge, value - change_date()
When selecting an Arrival Date on the form, the Departure Date is automatically populated with this date plus one day.
Please Log in or Create an account to join the conversation.
- Topic Author
- Robert Lanzone
- Offline
- Fresh Boarder
- Posts: 12
- Thank you received: 1
9 years 3 months ago
Support replied the topic: Populating Date Field using Form attribute
Dear Rob,
Please be informed, that we have added the following code to the custom Javascript section of your first Test form:
function change_date()
{
d =document.getElementById('wdform_4_element25').value;
date = new Date();
parts = d.split('-');
date .setMonth(parts[1]-1);
date .setFullYear(parts[0]);
date .setDate(parts[2]);
date.setTime(date.getTime() + 86400000);
var dte_string = date.getFullYear() + '-' + ("0" + (date.getMonth() + 1)).slice(-2) + '-' + ("0" + date.getDay());
wdformjQuery('#wdform_5_element25').val(dte_string );
}
and it appears to be working. So you can use that code.
As for your own script, could you please let us know what are you trying to achieve with it?
Thank you.
Best regards,
Web-Dorado team
Joomla extensions ,
Wordpress plugins ,
Drupal modules
Please Log in or Create an account to join the conversation.
9 years 3 months ago
Robert Lanzone replied the topic: Populating Date Field using Form attribute
Your code does update the second date field based on a change to the first date field, however, the updated date to the second date field does not represent a consistent offset in days from the original date field.
i.e. 2014-12-31 yields 2015-01-04 (plus 4 days)
2014-12-12 yields 2014-12-06 (minus 6 days)
2015-01-30 yields 2015-01-06 (minus 24 days)
The resulting date offset is not consistent.
My original request was to subtract 180 days from first date field, but the code I attached to this thread takes the first date field and simply adds 1 day to it.
So 2014-12-31 yields 2015-01-01
2014-12-19 yields 2014-12-20
The number of days to offset by can be done simply be by adjusting:
d.setTime(d.getTime() + 86400000);
to use a + or - and a multiplier to 86400000 to do more than one day.
d.setTime(d.getTime() + (86400000 * 3)); - for three days ahead in second date field.
I think your code has the math right, but I think there is something wrong with the subsequent parsing to assemble the date in the second field. I don't know exactly what.
My working form with add one day to first date filed and output in second date field is at:
www.traveltipsandpicks.com/my-profile/test-form
The bad parsing of date form is at:
www.traveltipsandpicks.com/my-profile/test-form-2
Please Log in or Create an account to join the conversation.
- Topic Author
- Robert Lanzone
- Offline
- Fresh Boarder
- Posts: 12
- Thank you received: 1
9 years 3 months ago
Support replied the topic: Populating Date Field using Form attribute
Dear Rob,
So as we understand, you need to have the code for second date, which gets autofilled, to have the ability to be multiplied and autofill the corresponding date? Such as, for your example, 180 days?
Thank you.
Best regards,
Web-Dorado team
Joomla extensions ,
Wordpress plugins ,
Drupal modules
Please Log in or Create an account to join the conversation.
9 years 3 months ago
Robert Lanzone replied the topic: Populating Date Field using Form attribute
Yes.
My two previous posts adjusted the code you nice folks provided to do the same. Thanks much.
~Rob
Please Log in or Create an account to join the conversation.
- Topic Author
- Robert Lanzone
- Offline
- Fresh Boarder
- Posts: 12
- Thank you received: 1
9 years 3 months ago
Support replied the topic: Populating Date Field using Form attribute
Dear Robert,
Please feel free to let us know in case you need any further help.
Thank you.
Best regards,
Web-Dorado team
Joomla extensions ,
Wordpress plugins ,
Drupal modules
Please Log in or Create an account to join the conversation.
9 years 3 months ago