Jquery does not date correctly

0

I did this and the msg is: Invalid Date . I think it's because everything turned string. How do I bring only the numeric part in the date? Incidentally, I say this, because I can not return a date in our format within jQuery . In 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 was the cast I made, but the date comes type:

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

I would like only one: 06/16/2014, without the time part

    
asked by anonymous 16.06.2014 / 19:15

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

  • x receives the date in the format string (DateTime of C # serialized in json, for example);
  • re is the regular expression pattern for separating numbers into a group (in this case the group at position 0 of vector m );
  • m is the vector with the result of the recognition of the re pattern (which the regex recognizes);
  • m[0] is the value, in epoch of the date;
  • parseInt converts to integer in base 10;
  • d is your new date.
  • In 1 line

    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 DataCadastro

    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);
    
        
    16.06.2014 / 19:26
    0

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

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

        
    16.06.2014 / 19:58