Skip to main content
added 4 characters in body
Source Link

I think maybe the format date in my custom date_form is different from format date in magento form php code , cause this problem , because when I changed this.outputDateTimeFormat parameter to for example : "YYYY/DD/MM HH:mm" , stored value was changed and it got valid format value ( for example : "2023-08-10 05:30:00" ) , but still it has different value from the value has sent and they aren't equivalent.

I think maybe the format date in my custom date_form is different from format date in magento form php code , cause this problem , because when I changed this.outputDateTimeFormat parameter to for example : "YYYY/DD/MM HH:mm" , stored value was changed and it got valid format value ( for example : "2023-08-10 05:30:00" ) , but still it has different value from the value sent and they aren't equivalent.

I think maybe the format date in my custom date_form is different from format date in magento form php code , cause this problem , because when I changed this.outputDateTimeFormat parameter to for example : "YYYY/DD/MM HH:mm" , stored value was changed and it got valid format value ( for example : "2023-08-10 05:30:00" ) , but still it has different value from the value has sent and they aren't equivalent.

added 2380 characters in body
Source Link

for this purpose I've added this linethese lines to my requirejs-config in my module :

   'map': {
       '*': {
          'mage/calendar' : 'MyNamespace_MyModule/js/persian_calendar',
         'Magento_Ui/js/form/element/date' : 'MyNamespace_MyModule/js/form/element/date_form'

        }
    }

persian date and gregorian date are completely different and they have different years , months and days name . I want to show persian date to user but save date as gregorian format to database .

my idea for sending date to server and saving it to database is :

for sending gregorian date to server I changed in my custom calendar.js when user select a date on calendar , persian date is showing to user at same time I put equivalent gregorian date in hidden field with same input name , finally I change the name of the previous field to something to have only one unique input nameonShiftedValueChange function , I do this in my custom date_form :

onShiftedValueChange: function (shiftedValue) {
        var value,
            formattedValue,
            formattedValueUTC,
            momentValue;

        // this is very simple code that check if selected date is gregorian date or persian date  
        var englishYears = ["2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026",
            "2027", "2028", "2029", "2030", "۲۰۱۸", "۲۰۱۹", "۲۰۲۰", "۲۰۲۱", "۲۰۲۲", "۲۰۲۳", "۲۰۲۴",
            "۲۰۲۵", "۲۰۲۶", "۲۰۲۷", "۲۰۲۸", "۲۰۲۹", "۲۰۳۰"];
        var englishDate = false;
        for (var i = 0; i < 26; i++) {
            if (shiftedValue.indexOf(englishYears[i]) > -1) {
                englishDate = true;
                break;
            }
        }

        if (shiftedValue) {

            // if selected date was persian,I converted it to equivalent gregorian format, for converting dates I used https://github.com/jalaali/moment-jalaali library 
            if (!englishDate) { 
                shiftedValue = jMoment.from(shiftedValue, 'fa', 'DD/MM/YYYY HH:mm').format(this.pickerDateTimeFormat);
            }

            // the rest codes are for magento itself
            momentValue = moment(shiftedValue, this.pickerDateTimeFormat);
            if (this.options.showsTime) {
                formattedValue = moment(momentValue).format(this.timezoneFormat);
                formattedValueUTC = moment.tz(formattedValue, this.storeTimeZone).tz('UTC');
                value = this.outputDateTimeToISO ?
                    formattedValueUTC.toISOString() :
                    formattedValueUTC.format(this.outputDateTimeFormat);
            } else {
                value = momentValue.format(this.outputDateFormat);
            }
        } else {
            value = '';
        }

        if (value !== this.value()) {
            this.value(value);
        }
    }

by these my calendar showing correctly to user andcode equivalent gregorian date putting in hidden fieldwas sent to server correctly ., but when I checking the value stored in database it's completely different and invalid , for example I sent this value : 4/14/2019 5:30 am , but magento convert it to : 0010-08-10 05:30:00 !!

but when I send datathink maybe the format date in my custom date_form is different from format date in magento form php code , cause this problem , because when I changed this.outputDateTimeFormat parameter to server invalid date with incorrectfor example : "YYYY/DD/MM HH:mm" , stored value will send to server and magento ignores my hidden fieldwas changed and it got valid format value ( for example : "2023-08-10 05:30:00" ) , but still it has different value from the value sent and they aren't equivalent.

for this purpose I've added this line to my requirejs-config in my module :

   'map': {
       '*': {
          'mage/calendar' : 'MyNamespace_MyModule/js/persian_calendar'
        }
    }

persian date and gregorian date are completely different and they have different years , months and days name . I want to show persian date to user but save date as gregorian format to database .

my idea for sending date to server and saving it to database is :

in my custom calendar.js when user select a date on calendar , persian date is showing to user at same time I put equivalent gregorian date in hidden field with same input name , finally I change the name of the previous field to something to have only one unique input name

by these my calendar showing correctly to user and equivalent gregorian date putting in hidden field correctly .

but when I send data form to server invalid date with incorrect value will send to server and magento ignores my hidden field .

for this purpose I've added these lines to my requirejs-config in my module :

   'map': {
       '*': {
          'mage/calendar' : 'MyNamespace_MyModule/js/persian_calendar',
         'Magento_Ui/js/form/element/date' : 'MyNamespace_MyModule/js/form/element/date_form'

        }
    }

persian date and gregorian date are completely different and they have different years , months and days name . I want to show persian date to user but save date as gregorian format to database .

for sending gregorian date to server I changed onShiftedValueChange function , I do this in my custom date_form :

onShiftedValueChange: function (shiftedValue) {
        var value,
            formattedValue,
            formattedValueUTC,
            momentValue;

        // this is very simple code that check if selected date is gregorian date or persian date  
        var englishYears = ["2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026",
            "2027", "2028", "2029", "2030", "۲۰۱۸", "۲۰۱۹", "۲۰۲۰", "۲۰۲۱", "۲۰۲۲", "۲۰۲۳", "۲۰۲۴",
            "۲۰۲۵", "۲۰۲۶", "۲۰۲۷", "۲۰۲۸", "۲۰۲۹", "۲۰۳۰"];
        var englishDate = false;
        for (var i = 0; i < 26; i++) {
            if (shiftedValue.indexOf(englishYears[i]) > -1) {
                englishDate = true;
                break;
            }
        }

        if (shiftedValue) {

            // if selected date was persian,I converted it to equivalent gregorian format, for converting dates I used https://github.com/jalaali/moment-jalaali library 
            if (!englishDate) { 
                shiftedValue = jMoment.from(shiftedValue, 'fa', 'DD/MM/YYYY HH:mm').format(this.pickerDateTimeFormat);
            }

            // the rest codes are for magento itself
            momentValue = moment(shiftedValue, this.pickerDateTimeFormat);
            if (this.options.showsTime) {
                formattedValue = moment(momentValue).format(this.timezoneFormat);
                formattedValueUTC = moment.tz(formattedValue, this.storeTimeZone).tz('UTC');
                value = this.outputDateTimeToISO ?
                    formattedValueUTC.toISOString() :
                    formattedValueUTC.format(this.outputDateTimeFormat);
            } else {
                value = momentValue.format(this.outputDateFormat);
            }
        } else {
            value = '';
        }

        if (value !== this.value()) {
            this.value(value);
        }
    }

by these code equivalent gregorian date was sent to server correctly , but when I checking the value stored in database it's completely different and invalid , for example I sent this value : 4/14/2019 5:30 am , but magento convert it to : 0010-08-10 05:30:00 !!

I think maybe the format date in my custom date_form is different from format date in magento form php code , cause this problem , because when I changed this.outputDateTimeFormat parameter to for example : "YYYY/DD/MM HH:mm" , stored value was changed and it got valid format value ( for example : "2023-08-10 05:30:00" ) , but still it has different value from the value sent and they aren't equivalent.

edited title
Link

magento 2 custom calendar instead of core magento calendar

Source Link
Loading