Only accept future dates by datetime picker (v2023)

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
sclg
User
Posts: 153
Location: UK

Only accept future dates by datetime picker (v2023)

Post by sclg »

How can I make the datetime picker (or a date field in general) only accept dates from today onwards - Refusing all dates before today?
Thanks


mobhar
User
Posts: 11922

Post by mobhar »

You may search in this forum by using minDate keyword.

Even though you can prevent to select date that previous than today via DateTimePicker, you still need validation from server side, since end-users are still be able to change the date by changing the value from the Textbox control.


sclg
User
Posts: 153
Location: UK

Post by sclg »

Thanks, but I was intending to make the date text box read-only so they had to use the DatePicker.
However I'm still stuck on getting today's date. Following ideas in other posts I've tried...

{restrictions:{"minDate":"today"}}
Unable to parse provided input: today, format: dd/MM/y

{restrictions:{"minDate":Moment()}}
Uncaught ReferenceError: Moment is not defined

{restrictions:{"minDate":moment()}}
Uncaught ReferenceError: moment is not defined

{restrictions:{"minDate":[]}}
Uncaught Error: TD: Could not correctly parse "" to a date for restrictions.minDate.

Any thoughts?


mobhar
User
Posts: 11922

Post by mobhar »

Change this code:
{restrictions:{"minDate":"today"}}

to:
{restrictions: {"minDate": "<?php echo CurrentDate(); ?>"} }

I tried with v2024, and it works properly. It should work also for v2023.

In addition, make sure you have already adjusted the locale setting (Tools -> Locale Settings) for date directive as "yyyy-MM-dd".


ArtwareSolutions
User
Posts: 10

Post by ArtwareSolutions »

In my case, if having "yyyy-MM-dd" is working as expected in v2024, but I need to achieve the same result having the date directive like "dd/MM/yyyy". Then, can this be achieved somehow with this setting?


mobhar
User
Posts: 11922

Post by mobhar »

I don't think so, since CurrentDate() global function by default will return current date in year-month-date format.


amsire2
User
Posts: 160

Post by amsire2 »

mobhar wrote:

Change this code:
{restrictions:{"minDate":"today"}}

to:
{restrictions: {"minDate": "<?php echo CurrentDate(); ?>"}}

Where should I place this code?

If I want to allow only today date and tomorrow date, how to do that?


mobhar
User
Posts: 11922

Post by mobhar »

Go to Tools -> Extensions -> DateTimePicker -> Advanced -> Fields -> find the related table and field -> Options.

You may set minDate as today's date and set maxDate as tomorrow's date.


amsire2
User
Posts: 160

Post by amsire2 »

I manage to set get the coding for minDate and maxDate

{restrictions: {"minDate": "<?php echo CurrentDate(); ?>"}}
{restrictions: {"minDate": "<?php echo date("Y-m-d", strtotime("tomorrow"))?>"}}

So how to put both of the coding in datepicker extension?


mobhar
User
Posts: 11922

Post by mobhar »

Wrong syntax. You don't need to write twice for restrictions keyword. You only need to write it once, and put it in one-line.


amsire2
User
Posts: 160

Post by amsire2 »

Thank you. i used this code and its work.

{restrictions: {"minDate": "<?php echo CurrentDate(); ?>","maxDate": "<?php echo date("Y-m-d", strtotime("tomorrow"))?>"}}


DGarza
User
Posts: 159

Post by DGarza »

mobhar wrote:

you still need validation from server side, since end-users are still be able to change the date by changing the value from the Textbox control.

For this, you can test this code, that works for me:

Jquery Code:

const currentDate = new Date().getTime();
$("IDDatetimepickerinput").change(function(){
    const inputdate = new Date(document.getElementById('IDDatetimepickerinput').value.split('/').join('-').split(' ')[0]).getTime(); //My Datetime format its "yyyy/MM/dd" that why I need to use split and join for change this "-" to this "/"(change this code for your format)

    if(inputdate > currentDate){
        Swal.fire({
        icon: 'error',
        title: 'Oops...',
        html: 'The date that you selected is a <strong class="text-danger">future date</strong>.',
        footer: 'Try with another date'
        })
        
        $(".tempus-dominus-widget").removeClass("show"); //Hide the calendar
        $("IDDatetimepickerinput").val(""); //Erase the text that you put on your input datetime
        $(".active").removeClass("active"); //Remove the selected date active of the calendar
    }
});

Post Reply