Search code examples
stringdate.net-coretype-conversionisodate

Stop formData string from being converted to a date string representation


This one is being hard to uncover. We have a .NET Core 2.2 app in which we have a form with many fields and a field for the user to attach some file.

Problem: one specific field CaqhNumber has a min and max length = 8.

When the user types in a value for example 20200706 the value that get's sent to the server is a date string representation of this value. So on the server side we get:

07/06/2020

Basically it thinks this specific field is an ISO date.

![Do

How can one stop this from happening? We just want to get the string on the server side the way it was entered on the UI side. It's not a date. It's plain string in both sides.

Something tells me this is related to the .NET Core configs.


Solution

  • Guess what... there was a piece of code behind the curtains messing up with the value:

    const isValidDate = moment(
            value,
            moment.ISO_8601,
            true
        ).isValid();
    
        if (isValidDate) {
            formData.append(
                key,
                moment(value).format('MM/DD/YYYY')
            );
    

    The fix:

    if (value instanceof Date) {
        formData.append(key, moment(value).format('MM/DD/YYYY'));