Issue with Moment.js plugin
Issue with Moment.js plugin
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
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:Can you give a link to a page showing the issue please.
Allan
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:
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:
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'.
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" ...
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
"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.