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 .

#19485

Please Log in or Create an account to join the conversation.


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
#19498

Please Log in or Create an account to join the conversation.


Robert Lanzone replied the topic: Populating Date Field using Form attribute

#19529

Please Log in or Create an account to join the conversation.


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
#19532

Please Log in or Create an account to join the conversation.


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.

#20453

Please Log in or Create an account to join the conversation.


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
#20485

Please Log in or Create an account to join the conversation.


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

#20499

Please Log in or Create an account to join the conversation.


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
#20523

Please Log in or Create an account to join the conversation.


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

#20531
The following user(s) said Thank You: Support

Please Log in or Create an account to join the conversation.


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
#20546

Please Log in or Create an account to join the conversation.


Time to create page: 0.231 seconds
Powered by Kunena Forum

WORLD-CLASS SUPPORT

We are here for you every step of the way. Turn to our support with any questions you might have about our products and services. Our experienced customer care team is available to help you as fast as we possibly can.

Contact us