Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to format min and max date in Laravel

In my Laravel-5.8, I ham using JQuery datepicker

<script>
   $(document).ready(function () {
   $('.leaveDate').datetimepicker({
   format: 'MM/DD/YYYY',
    locale: 'en'
  });
<script>
   $(document).ready(function () {
   $('.dobDate').datetimepicker({
   format: 'MM/DD/YYYY',
    locale: 'en'
  });
</script>

How do I format

  1. For leaveDate: the minimum date to be 1st day of the current year (users cannot select before 1st day of the current year) and maximum date to be last day of the current year(user cannot select after last after last day of the current year)

  2. for dobDate: users cannot select future date

Thanks

0 likes
3 replies
ismaile's avatar

You should use minDate and maxDate. Here is the documentation for jQuery date time picker: https://xdsoft.net/jqplugins/datetimepicker/#mindate if that's the one you use.

$('.leaveDate').datetimepicker({
   format: 'MM/DD/YYYY',
    locale: 'en',
    minDate: '2020/01/01',
    maxDate: '2020/12/31'
});
$('.dobDate').datetimepicker({
   format: 'MM/DD/YYYY',
    locale: 'en',
    maxDate: (new Date()).toString()
  });
MichalOravec's avatar

I don't know which exactly plugin you use, but in this one you can set startDate and endDate

https://www.daterangepicker.com/

In pure js

new Date(new Date().getFullYear(), 0, 1); // first day of current year

new Date(new Date().getFullYear(), 11, 31); // last day of current year

new Date(); // current day so it's max date

Or you cas use https://momentjs.com/ which is similar to Carbon but in js world

Snapey's avatar

Not a Laravel Question. Try this;

<script>
   $(document).ready(function () {
     $('.leaveDate').datetimepicker({
       format: 'MM/DD/YYYY',
       changeYear: false,
       locale: 'en'
    });

     $('.dobDate').datetimepicker({
       format: 'MM/DD/YYYY',
       maxDate:0,
       locale: 'en'
    });

  });

Current year is most easily accomplished by not letting the user change the year

DOB is most easily accomplished as an offset 0 days from today.

Both are theoretical based on reading the API

https://api.jqueryui.com/datepicker/#option-maxDate

Please or to participate in this conversation.