Issue with Moment.js plugin

Issue with Moment.js plugin

rf1234rf1234 Posts: 3,028Questions: 88Answers: 422

Hi Allan,
with the fix provided by you the moment.js plugin works fine with format: 'L' now.

I also need to use format: 'LLL' though and this causes another problem.

While moment.js properly returns "11. Februar 2017 11:31" as "tomorrow" from this

momentLocale = 'de'; //used in Editor
moment.locale('de');
tomorrow = moment(currentTime).add(1, 'day').format('LLL');

there is however an issue using it in Editor:

{
                label: "End of Bid Period:",
                name:  "rfp.bidtime_end",
                type:  "datetime",
                def:   function () { return tomorrow },
                format: 'LLL',
                opts:  {
                    showWeekNumber: true,
                    momentLocale: momentLocale
                }
            }

The default works fine and displays "11. Februar 2017 11:31". If I pick a different date time and want to save it Editor sends this to the server: 0000-00-00 00:00:00. I could see it in my debugger. I tried with format: 'L'. That worked fine but it is not the format I need in this case. So the problem is really about the 'LLL' and Editor. I would really need most of these formats to work:
https://momentjs.com/
Please help.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,852Questions: 1Answers: 10,519 Site admin
    Answer ✓

    I've just tried LLL with this example and it appears to work as expected for me. This is what is sent to the server:

    data[row_11][updated_date]:February 11, 2017 4:00 PM

    Can you give a link to a page showing the issue please.

    Allan

  • rf1234rf1234 Posts: 3,028Questions: 88Answers: 422
    edited February 2017

    Allan, you are right: In the English US format this works, also in the English UK format of:
    17 February 2017 10:48 (just copied from my debugger). But it does not work with the German format! I might need to transform it before it is being sent to the server.

    Internationalization is a nightmare I am afraid.
    Want more? Here is something from the server side:
    Php properly transforms the German format of 18.02.2017 into a timestamp using this statement:

    return date( 'Y-m-d H:i:s', strtotime( $val ) );
    

    But if you use the English UK format of 18/02/2017 it returns: "1970-01-01 01:00:00" because PHP seems to interpret this date the American way: 2nd day of the 18th month which is invalid. With the periods it obviously interprets it the "European" way.

    For English UK I only needed to make the change on the PHP side. There was no problem with the 'LLL' for English UK:

    $dateTime = $val;
        $dateTime = str_replace('/', '-', $dateTime);
        $dateTime = str_replace('.', '-', $dateTime);    
        
        return date( 'Y-m-d H:i:s', strtotime( $dateTime ) );
    

    To be safe I also transformed the German periods into hyphens.

    If I set the locale to German this is being sent to the server for format LLL:
    15. Februar 2017 11:45 (I was obviously wrong before; the problem of the zeroes was probably caused by another issue which has been resolved in the meantime ...).

    If I set the locale to English UK this is being sent for format LLL:
    15 February 2017 11:45

    It turns out that PHP does not like the "Februar" it insists on "February" and moment.js does not do the transformation either before the field is sent to the server.

    With "February" PHP returns the correct timestamp. With "Februar" it returns "1970-01-01 01:00:00".

    This function does all the server side transformations now for German (Germany) and English (UK). This is for moment.js locales 'de' and 'en-gb' and both formats 'L' and 'LLL'.

    function setFormatterDate(&$val) {
        $dateTime = $val;
        $dateTime = str_replace(
            ['Januar ', 'Februar ', 'März ', 'April ', 'Mai ', 'Juni ', 'Juli ',
                'August ', 'September ', 'Oktober ', 'November ', 'Dezember '],
            ['January ', 'February ', 'March ', 'April ', 'May ', 'June ', 'July ',
                'August ', 'September ', 'October ', 'November ', 'December '],
            $dateTime);
        $dateTime = str_replace('/', '-', $dateTime);
        $dateTime = str_replace('.', '-', $dateTime);    
        
        return date( 'Y-m-d H:i:s', strtotime( $dateTime ) );
    }
    

    Be sure to have a space after the month names because otherwise "January" will be transformed into "Januaryy" which will make PHP return "1970-01-01 01:00:00" ...

  • allanallan Posts: 63,852Questions: 1Answers: 10,519 Site admin
    Answer ✓

    But it does not work with the German format! I might need to transform it before it is being sent to the server.

    Actually, I would suggest the inverse. Have the server use a different format for its formatter based on the user's language session. Or you could have the client-side tell the server-side what language it is submitting in, and dynamically change the formatter (and validator) based on that.

    Allan

  • rf1234rf1234 Posts: 3,028Questions: 88Answers: 422

    "Have the server use a different format for its formatter based on the user's language session."
    Agreed and that is what I eventually implemented. The function in the box above does exactly that. Depending on the language it does string replacements. The first str_replace with the months will only modify German 'LLL'-dates. The second will only modify English UK 'L'-dates and the third will modify German 'L'-dates. For that reason I don't need to explicitly check the language in that function.

This discussion has been closed.