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

ellajhonm's avatar

Unable to Change Date Format for Datepicker

Our team currently uses a bootstrap template for our registration page and the datepicker that comes with it. The datepicker formats a selected date as DD/MM/YYYY. Of course, SQL uses YYYY-MM-DD, so submitting the date with the datepicker's format creates an error.

Apparently the datepicker (created by Dan Grossman) uses Moment.js, and I tried changing the format in its datepicker.js from

this.locale = {
            format: moment.localeData().longDateFormat('L')
}

to

this.locale = {
	  format: moment.format('YYYY-MM-DD')
}

It didn't work. I tried looking for its other js files and found global.js. Changed the date formats indicated in the following from

try {
        $('.js-datepicker').daterangepicker({
            "singleDatePicker": true,
            "showDropdowns": true,
            "autoUpdateInput": false,
            locale: {
                format: 'DD/MM/YYYY'
            },
        });
    
        var myCalendar = $('.js-datepicker');
        var isClick = 0;
    
        $(window).on('click',function(){
            isClick = 0;
        });
    
        $(myCalendar).on('apply.daterangepicker',function(ev, picker){
            isClick = 0;
            $(this).val(picker.startDate.format('DD/MM/YYYY'));
    
        });
    
        $('.js-btn-calendar').on('click',function(e){
            e.stopPropagation();
    
            if(isClick === 1) isClick = 0;
            else if(isClick === 0) isClick = 1;
    
            if (isClick === 1) {
                myCalendar.focus();
            }
        });
    
        $(myCalendar).on('click',function(e){
            e.stopPropagation();
            isClick = 1;
        });
    
        $('.daterangepicker').on('click',function(e){
            e.stopPropagation();
        });
    
    
    } catch(er) {console.log(er);}
    /*[ Select 2 Config ]
        ===========================================================*/
    
    try {
        var selectSimple = $('.js-select-simple');
    
        selectSimple.each(function () {
            var that = $(this);
            var selectBox = that.find('select');
            var selectDropdown = that.find('.select-dropdown');
            selectBox.select2({
                dropdownParent: selectDropdown
            });
        });
    
    } catch (err) {
        console.log(err);
    }

to

 try {
        $('.js-datepicker').daterangepicker({
            "singleDatePicker": true,
            "showDropdowns": true,
            "autoUpdateInput": false,
            locale: {
                format: 'YYYY-MM-DD'
            },
        });

        var myCalendar = $('.js-datepicker');
        var isClick = 0;

        $(window).on('click',function(){
            isClick = 0;
        });

        $(myCalendar).on('apply.daterangepicker',function(ev, picker){
            isClick = 0;
            $(this).val(picker.startDate.format('YYYY-MM-DD'));

        });

        $('.js-btn-calendar').on('click',function(e){
            e.stopPropagation();

            if(isClick === 1) isClick = 0;
            else if(isClick === 0) isClick = 1;

            if (isClick === 1) {
                myCalendar.focus();
            }
        });

        $(myCalendar).on('click',function(e){
            e.stopPropagation();
            isClick = 1;
        });

        $('.daterangepicker').on('click',function(e){
            e.stopPropagation();
        });


    } catch(er) {console.log(er);}

Nothing changes. I am still very new to JavaScript and I have no idea what to do. What other solutions can I try?

UPDATE: I tried clearing the cache thinking that may be why nothing seems to change, but now the datepicker won't appear ^^;

0 likes
8 replies
jlrdw's avatar

Where are you inserting data, that's where you need correct format.

ellajhonm's avatar

@jlrdw Do you mean the <input> tag or controller (I'm using Laravel atm) ?

jlrdw's avatar

No the method (function) where you insert.

What is Laravel atm?

ellajhonm's avatar

@jlrdw Sorry, I meant that I use Laravel "at the moment" .

So the datepicker is for the user's birthdate and everything goes here in my store() function In RegistrationController:

 public function store(){
        $validated = request()->validate([
            'date_of_birth' => ['required', 'dateformat:d/m/Y'],
            'gender' => ['required'],
            'email' => ['required', 'email', 'max:255', 'unique:users'],
            'contact_number' => ['required', 'digits:11'],
            'password' => ['string',Password::min(8)],
        ]);

        $attributes = array_merge($validated, [
            'user_type_id' => 3,
        ]);

        $newUser = User::create($attributes);
 				
		return redirect('/');

    }

I also tried adding a mutator to my User model, but it doesn't seem to do anything:

 public function setBirthDateAttribute($value)
    {
        $this->attributes['date_of_birth'] = Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d');
    }
MohamedTammam's avatar
Level 51

@ellajhonm according to your validation list, it should be setDateOfBirthAttribute

 public function setDateOfBirthAttribute($value)
    {
        $this->attributes['date_of_birth'] = Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d');
    }
1 like
ellajhonm's avatar

@MohamedTammam This worked! Thank you! I have to keep in mind how important name conventions are in Laravel

1 like
jlrdw's avatar

What happens if format directly in the store method. Maybe mutator isn't setup correct.

1 like

Please or to participate in this conversation.