Skip Navigation

[Resolved] Presetting date/time field results in weird behaviour

This thread is resolved. Here is a description of the problem and solution.

Problem:

Preset the date/time field with the current date/time and prevent selection of dates in the future.

Solution:

It needs custom JS codes, for example:

Relevant Documentation:

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by davidS-53 5 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1202917
2019-02-20 21_06_21-Safety Check – 1.Test alarm pager to ensure there is sufficient credit and txts .png

Following on from this thread- (I thought I'd solved it but only just got round to testing) https://toolset.com/forums/topic/setting-maxdate-on-date-field-in-cred-form-resets-default-value/

I have adapted some JQuery that restricts the possible values of a datetime CRED field to past dates. It also presets both the date and time fields to the current datetime. Here's my code:

		$('input[name="wpcf-date-checked[display-only]"]').datepicker( 'option', {minDate: '-5y', maxDate: 0} );
		$('input[name="wpcf-date-checked[display-only]"]').datepicker( 'option', 'dateFormat', 'MM d, yy' );
		$('input[name="wpcf-date-checked[display-only]"]').datepicker( 'setDate', new Date() );

		var dt = new Date();
		var hour = dt.getHours();
		var minute = dt.getMinutes();

		$('select[name="wpcf-date-checked[hour]"]').val(hour);
		$('select[name="wpcf-date-checked[minute]"]').val(minute);

The problem I have just discovered is that if I now use the datepicker to change the date to a past date, it restricts the date correctly but attempts to set the value of the field to November 30 1999 (see screenshot)? Any idea why that may be?

#1203537

Hello,

This is abnormal, I have tried your JS codes codes in my localhost with a fresh wordpress installation + the latest version of Toolset plugins, it works fine, there isn't similar problem as you mentioned above:
use the datepicker to change the date to a past date, it restricts the date correctly but attempts to set the value of the field to November 30 1999

Please try these:
1) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/

2) In case it is a compatibility problem, please deactivate other plugins, and switch to wordpress default theme 2019, and test again

3) Also check if there is any JS errors in your website.
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/#javascript-debugging

#1205991

Hi Luo, I have followed your suggestions above and the problem persists.

I have also tried it and confirmed the issue exists on a fresh install with just toolset, 2019 and basic post types for testing set up.

#1206128

I have tried your JS codes in below test site, but there isn't the same problem:

use the datepicker to change the date to a past date, it restricts the date correctly but attempts to set the value of the field to November 30 1999

Test site:
hidden link
username/password: xgren/111111

Please duplicate the same problem in above test site, thanks

#1206587
2019-02-27 11_05_11-.png

Luo, the demo site you've pointed me to has two errors.

One: The date field is not prepopulated with today's date.

Two: The date field allows the selection of future dates.

See screenshot.

Quite simply, all I am trying to achieve is a) preset the date/time field with the current date/time and b) prevent selection of dates in the future.

#1206779

Those two errors you mentioned above are from your custom JS codes, I only copy/paste them into the demo site.

For the new question:
a) preset the date/time field with the current date/time and
b) prevent selection of dates in the future.

It needs custom JS codes, for example:

jQuery( document ).ready(function($) {
   setTimeout(function() {
     $('input[name="wpcf-date-checked[display-only]"]').datepicker("option", "minDate", "-5Y");
     $('input[name="wpcf-date-checked[display-only]"]').datepicker("option", "maxDate", "0" );
     $('input[name="wpcf-date-checked[display-only]"]').val( new Date() );
    }, 500 );
  
  var dt = new Date();
  var hour = dt.getHours();
  var minute = dt.getMinutes();

  $('select[name="wpcf-date-checked[hour]"]').val(hour);
  $('select[name="wpcf-date-checked[minute]"]').val(minute);
  
});

See the result here:
hidden link

#1207303
2019-02-28 10_15_54-.png

Sweet so I see you've added a timeout to it, it now presets the date field. However, the prepopulated value now has the time in the date field as well (see screenshot). It displays correctly if you then change the value.

How would one get the initial date value displaying correctly? I assume I need to format the Date() value?

#1207418

Yes, you need to format the date value, for example:

var my_date = $.datepicker.formatDate( 'MM d, yy', new Date() );
console.log(my_date );

You should be able to see the formatted date value in your browser console window

See jQuery document:
hidden link

#1207419

Perfect, cheers.