The problem:
Trying to add rental product to the cart, fails because of invalid Start or/and End Date.
Let me explain when:
When my Region and Language Format is set to English (United States) and my Short Date Format is set to "dd/MM/yyyy"
On our server, going to Control Panel -> Clock, Language, and Region -> Region and Language opens the Region and Language popup. Clicking on the additional settings opens Customize Format popup. Choosing the Date tab shows all date format settings.
We like our format to be day/month/year (e.g. 24.12.2014). And for short date format we set "dd/MM/yyyy", then save it. Keep in mind that the Region and Language Format is set to English (United States).
Now start your application and go to rental product details page. Choose start and end date. Use day(s) that is(are) above 12th. Now you will not be able to add the product.
Problematic code is in file:
\Presentation\Nop.Web\Controllers\ShoppingCartController.cs
Method: ParseRentalDates
After we debugged the code, we saw that the Date.Parse method returns NULL on the attempt to add rental product to the cart.
The problem comes from using specific culture - "en-US":
var formatProvider = new CultureInfo("en-US");
On our machine it expects the format "dd/MM/yyyy", but in the _RentalInfo.cshtml the format is set to "M/dd/yyyy".
The Date.Parse method tries to parse the format as day / month / year (because this is the machine`s date format), but the date is passed as month/day/year (e.g.08/25/2015) and, of course, there is no month with number 25...
And here the parsing fails with error: [System.FormatException] = {"String was not recognized as a valid DateTime."}.
We think every administrator is free to change the date format on his machine. And he can do just like us - use the en-US culture and "dd/MM/yyyy" date format.
Therefore the code must not use specific culture, because this can lead to similar errors.
We have a fix for this issue:
Considering we set "M/dd/yyyy" format in the _RentalInfo.cshtml file, we can use the same format for parsing the date in the ParseRentalDates method.
So, everything inside the try{} clause inside the ParseRentalDates method can be substituted by this code:
startDate = DateTime.ParseExact(ctrlStartDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
endDate = DateTime.ParseExact(ctrlEndDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
The problem is reproduced on:
nopCommerce 3.50 both source and no-source code version, downloaded by the official site, without any modifications.
SQL: both MS SQL Server 2008 R2 and MS SQL Server 2012
OS: Windows 7 Ultimate SP1
We hope that everything is understandable. If something is unclear, please write back for more clarifications.