1

I am having a strange issue with Dates in JS and C# (asp.net core 2.1). I am adding dates on the front end and trying to post them back to the server with the following JavaScript:

$("#editOperation").on("submit",
    function(e) {
        e.preventDefault();
        var form = $(this);
        var url = form.attr("action");

        // change date to YYYY-MM-DD format
        for (let i = 0; i < form[0].length; i++) {
            if (form[0][i].className.includes("hdn-exclude-")) {
                if (form[0][i].value) {
                    var date = moment(form[0][i].value);
                    form[0][i].value = date.format('YYYY-MM-DD');
                }
            }
        }

        var formData = new FormData(form.get(0));
        $.ajax({
            url: url,
            data: formData,
            type: "post",
            contentType: false,
            processData: false,
            success: function (response) {
                if (response.result) {
                    toastr.success("Success!");
                    setTimeout(function () {
                            window.location = $("#cancelEditOperation").attr("href");
                        },
                        1000);
                } else {
                    toastr.error(response.errorMessage);
                }
            }
        });
    });

However the issue I am getting is when a Date that has a day PAST the 12th of any given month (e.g 13/07/2018 - Australian format) then C# will what seems like not be able to parse it and give it a default value of 1/01/0001 12:00:00 AM.

If the date has a day of BEFORE or ON the 12th of any given month (e.g 12/06/2018) then it will be correct on the C# side as 6/12/2018 12:00:00 AM

This is my model in C#:

public class GetOperationExcludeDate
{
    public DateTime StartDate { get; set; }

    public DateTime EndDate { get; set; }
}

And my Action:

[HttpPost("EditOperation/{id}")]
[AutoValidateAntiforgeryToken]
public async Task<ApiBooleanResponse> EditOperation(string id, GetOperationExcludeDate model)
{
    return await _transportCompanyService.EditServiceOperationAsync(id, model);
}

How am I able to normalize my Dates so that C# will recognize the format depending on the region and parse it correctly? (I can see that it is trying to parse it as an American format when given an Australian format)

0

1 Answer 1

3

Send your dates in the ISO 8601 format.

Using the Javascript Date object, you have:

var isoDate = myDate.toISOString(); // returns "2018-06-12T01:58:40.867Z"

or using Moment:

var isoDate = momentDate.format(); // returns "2018-06-12T10:58:40+09:00"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.