Jquery does not mount date correctly

Asked

Viewed 558 times

0

I did that and MSG is: Invalid Date. I think it’s because everything has turned into a string. How do I bring only the numerical part on the date? In fact, I say this because I cannot return a date in our format within the jQuery. In the Alert() gives Invalid Date.

var dtCadastro = data.agendamento.DataCadastro.substring(6, data.agendamento.DataCadastro.length - 2);
var dtAgendanmento = data.agendamento.DataAgendamento.substring(6, data.agendamento.DataAgendamento.length - 2);
var dtVisita = data.agendamento.DataVisita.substring(6, data.agendamento.DataVisita.length - 2);

var dt = new Date(dtCadastro);

alert(dt);

var dtCadastro = (Date)(data.agendamento.DataCadastro.substring(6, data.agendamento.DataCadastro.length - 2));

This is the cast I did, but the date’s like:

Mon Jun 16 2014 14:24:45 GMT....

I would like just one: 16/06/2014, without the hour part

  • I did a pada cast Date, then I can bring the date, but it comes in a format almost extensive and I would like it to be like this: dd/MM/yyyy only. I will edit the post to show the cast

  • It’s easier for you to format the date in C#. You have the C# code snippet that returns the value so I can help?

2 answers

2


Use:

var x = "/Date(1402369200000)/";
var re = /-?\d+/; 
var m = re.exec(x); 
var d = new Date(parseInt(m[0], 10));
console.log(d);

// output: Tue Jun 10 2014 00:00:00 GMT-0300 (Hora oficial do Brasil)

Explanation

  1. x takes the date in string format (Datetime of C# serialized in json, for example);
  2. re is the regular expression pattern for separating numbers into a group (in this case the group at the position 0 vector m);
  3. m is the vector with the pattern recognition result re (what the regex recognizes);
  4. m[0] is the value, in date epoch;
  5. parseInt converts to integer in base 10;
  6. d is your new date.

In 1 row

var d = new Date(parseInt(/-?\d+/.exec("/Date(1402369200000)/")[0], 10));
console.log(d);

To format

  • d.getDate() + "/" + (d.getMonth()+1) + "/" + d.getFullYear() -> "10/6/2014"
  • d.toDateString() -> "Tue Jun 10 2014"
  • d.toLocaleDateString() -> "10/6/2014"

Example with Dataregistration

function getFormattedDate(date){
 var re = /-?\d+/; 
 var m = re.exec(date); 
 var d = new Date(parseInt(m[0], 10));
 return d.getDate() + "/" + (d.getMonth()+1) + "/" + d.getFullYear();
}

var dCad = getFormattedDate(data.agendamento.DataCadastro);
console.log(dCad);
  • 1

    To format you can use: d.toLocaleDateString(). Will return: '6/10/2014'. To add time, you use: d.toLocaleString(). Or you can format it the way you need it with: d.getDate() + "/" + d.getMonth() + "/" + d.getFullYear()

  • I did so, Maia and formatted. But now the three date fields are coming with the same date all(16/05/2014) and this date does not even exist in my comic book. When I break point the controller and see the date that is coming, there are three different dates and they are the correct dates, but after I pick up jquery via json, here comes this incorrect date and fill in all the dates with this value (incorrect).

  • The best thing would be to send the date already formatted in C# (as Joao Paulo suggested). For cases where the dates are wrong, it may be that some variables are being applied incorrectly. I need to see more code to know what’s going on.

  • See the example with Dataregistration that I put in. Maybe it helps you.

  • 1

    See, as I said in C# it’s okay, the dates are the valid ones that exist in the comic. When serializo and take jquery and do these formatting is zoa everything.

  • Solved Mayan according to his last reply. Thanks

Show 1 more comment

0

It is easier for you to format the date in C#.

Example: String.Format("{0:MM/dd/yyyy}", Variaveldadata);

  • In C# it is correct. The question is in the serelization for json and take jquery. But I solved with the example of the Mayan, creating a function to clean the "dirty date" and then take it in the main function.

Browser other questions tagged

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