0

I have a website with multiple locales (English, Dutch, French, German and Spanish). I'm saving dates on a front-end form through Ajax, but it seems that Craft saves the dates in the locales' format, eg. in English, the 1st February 2018 would be 02/01/2018 and in Dutch it would be 01/02/2018.

I'd think Craft would save all dates in the same format, disregarding any locale. I read somewhere that craft saves all dates in UTC format.

When I'm in the locale EN and I pass the date 01/02/2018, expecting 2nd January 2018 to be saved, it saves OK. However, if I'm in NL, DE or FR and I pass the same date, it is saved as 1st February 2018 as these languages switch the day and month around.

I always pass MM/DD/YYYY to the date field whatever locale. I'm using the dateFns library for formatting and the date it formats is correct. I use VueJS v-calendar as a datepicker which also shows everything correct.

But when it's saved to Craft it suddenly switched day and month in the back-end!

Any help or explanation, please? Seems like strange behavior choices.

0

Where are you seeing the incorrect date, in the database or when you output it in the templates?

I think Craft is probably working as expected here.

Craft will expect the date submitted to be in the correct format for the locale it was submitted from, so will translate it from that format into UTC to save in the database.

Then, when the date is output in templates, it is transformed again to the correct format for the locale it is being displayed in.

The EN locale presumably assumes US date formatting so 01/02/2018 is correct as 2nd January 2018. For European locales, a date submitted as 01/02/2018 will correctly be recognised as 2nd February 2018.

You need to make sure that the date input in your form is formatted according to the expected way for the locale the form is in.

  • I see it in the database. But isn't this a strange design choice? Because an input date field expects a certain format, it doesn't care about locales. And on top of that, the browser formats the date in the date field according to the users browser language, which can be different from the locale of the website. It probably is most of the time. So I would expect Craft to save it in a fixed locale (maybe if the field would be translatable it could do what it does now, then it would make sense). This behavior is good for Twig outputs but that's not always the use case! – Tom De Smet Sep 25 '18 at 12:55
  • If you're seeing the "incorrect" date in the database, then I think that confirms what I said. I actually think this is logical behaviour from a user point of view. A user is going to expect to input a date in a format familiar to them which is the same format as they will expect to view dates. Craft is ensuring that this happens. Craft is actually saving in a fixed locale in the database, i.e. UTC format. – Steve Rowling Sep 25 '18 at 12:56
  • But the formatting of the date the user is inputting isn't controlled by Craft, it's controlled by the browser. If I set my browser to English I see 09/25/2018. If I set it to Dutch I see 25.09.2018. So the user doesn't care what Craft saves it as. The date field however does, it expects the format MM/DD/YYYY. So if Craft switches DD and MM for the users benefit, the browser and the datefield interprets the date wrong! – Tom De Smet Sep 25 '18 at 13:07
  • Can you explain how you are setting the browser to English or Dutch? I'm not sure I understand what you mean by that. Do you mean that you are visiting the English or Dutch version of the site? Have you tested this in several browsers or just one? If several, do you get the same behaviour in all browsers? – Steve Rowling Sep 25 '18 at 13:56
  • Also, are you using a date input field or a text input field? – Steve Rowling Sep 25 '18 at 14:03
0

The answer of Steve Rowling was correct. For a similar official answer by Brandon you can go here: https://github.com/craftcms/cms/issues/3325

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.