Date reassembly with javascript

Asked

Viewed 388 times

1

In my bank I have a Datetime field. In my form I have three combos for: Day, Month and Year. I have a Javascript function, which serializes the information by sending it to my record controller. It turns out the date is coming null. I tried to do it like this, but it doesn’t work.

DataNascimento:
    ($("input[name='txtAno']").val() + "-" +
    $("input[name='txtMes']").val() + "-" +
    $("input[name='txtDia']").val()),

How I remodel the date in javascript?

3 answers

2

Well, your question is "How I remodel the date in javascript?" And I say to you, it’s simple, to mount a date you have the Javascript Date Object:

var a = $("input[name='txtAno']").val(); //ano
var m = $("input[name='txtMes']").val(); //mes
var d = $("input[name='txtDia']").val(); //dia
var data = new Date(a,(m-1),d); //o m-1 é porque o mês do javascript é zero-indexed.

Soon you will have a date reassembled in javascript, and you can use for its function.

0

I am seeing that you already have the data inputs dismembered, you could create an action, that instead of receiving a date, also receive the parts dismembered:

public ActionResult MinhaAction(int txtAno, int txtMes, int txtDia)
{
    var data = new DateTime(txtAno, txtMes, txtDia);
}

0

I found a bibliography of js on the internet the other day that helped me a lot with problems related to date in Javascript. Follow the link to download the library:

DATE JS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.