1

I need to convert my string to a date format to pass it in a way that my mvc c# understands it.

Please read the following, where I explain the problem step by step.

I have an input

<input name="EgresoStJohns" class="form-control txtOut"/>

When the user saves the form, I do the following in Jquery

var dateOut = $(".txtOut").val();

dateOut becomes something like this, depending on the date that the user picks: 30/03/2017

Then I try to parse my date, so the controller in c# mvc understands that I want to send a dd/mm/yy, otherwise, if I send 30/03/2017 the controller will understand month=30 and it will conver the entire date to null.

var DateToSend = new Date(parseInt(dateOut));
DateToSend = DateToSend.getDate() + "/" + (DateToSend.getMonth() + 1) + "/" + DateToSend.getFullYear();

However, inmmediately after doing the New Date, the input ALWAYS becomes "31/12/1969", so I am unable to work with it.

What can I do to solve this inconvenience?

5
  • Why are you passing dateOut to parseInt()? Commented Oct 11, 2017 at 22:05
  • parseInt(dateOut) return 30 - you would need to split the value based on / to get the components Commented Oct 11, 2017 at 22:05
  • @Pointy if I dont parseInt, I get "NaN" Commented Oct 11, 2017 at 22:07
  • Well getting the completely wrong date isn't much better than that. Search for date parsing questions here - there are thousands of them. Commented Oct 11, 2017 at 22:09
  • Of course it would be better to just change the culture on the server to one that expects dates in dd/MM/yyyy format, or to create a custom ModelBinder for dates Commented Oct 11, 2017 at 22:09

2 Answers 2

3

If you're certain that will be the entered date format, you could do something like this. All it's doing is reversing the month/day prior to creating the new Date object.

var dateOut = $(".txtOut").val().split("/");
var dateToSend = new Date(dateOut[1] + "/" + dateOut[0] + "/" + dateOut[2]);
Sign up to request clarification or add additional context in comments.

1 Comment

To solve my problem, I just put the month first so c# would understand it
1

you could try to use moment.js to parse your data in your client side.

First is to make the field type="date" then we can modify the field to format it DD/MM/YYYY by using moment.js

Second is we can add CSS to tweak the date field

then we can get the value by using moment.js again to format of MM/DD/YYYY

//format field
$(".txtOut").on("change", function() {
    this.setAttribute(
        "data-date",
        moment(this.value, "YYYY-MM-DD")
        .format( this.getAttribute("data-date-format") )
    )
}).trigger("change")


$(function(e){
  $("#btn").click(function(e){
     //parse date using moment.js, then use this to pass in your ajax.
     var DateToSend = moment($(".txtOut").val()).format("MM/DD/YYYY");     
     console.log(DateToSend);
     //format it whatever you want base on this docs https://momentjs.com/docs/
     console.log(moment($(".txtOut").val()).format("MMMM DD, YYYY"))
  })
})
.txtOut {
    position: relative;
    width: 150px; height: 20px;
    color: white;
}

.txtOut:before {
    position: absolute;
    top: 3px; left: 3px;
    content: attr(data-date);
    display: inline-block;
    color: black;
}

.txtOut::-webkit-datetime-edit, input::-webkit-inner-spin-button, input::-webkit-clear-button {
    display: none;
}

.txtOut::-webkit-calendar-picker-indicator {
    position: absolute;
    top: 3px;
    right: 0;
    color: black;
    opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>
<input name="EgresoStJohns" class="form-control txtOut" type="date" data-date="" data-date-format="DD/MM/YYYY" value="2017-03-30">
<button id="btn">Click me</button>

1 Comment

Thanks a lot for the detailed question, I really apreciate the time you took. However, I´m going with the other answer since it´s a more simple way to solve my specific problem.

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.