Convert string to date [duplicate]

8

I need to make a function increment X days to a date. The problem is that the date is coming as string in this format: dd/mm/yyyy and at the time of assigning the X days to that date, says that it does not recognize the var that will be incremented by X days, such as var_tal is not a function . How do I proceed? here is the example in the fiddle.

That way it does not work:

var dt_exclusao = '26/11/2015';
var periodo = 60;
var nova_data = new Date(dt_exclusao);
    //alert(dt_exclusao.setDate(dt_exclusao.getDate() + periodo));
    alert(nova_data);
    alert(periodo);
    nova_data.setDate(nova_data.getDate() + periodo);
    alert(nova_data);

But that's how it works, that means the past format is the problem:

var dt_exclusao = '2015-11-26T00:00:00';
var periodo = 60;
var nova_data = new Date(dt_exclusao);
    //alert(dt_exclusao.setDate(dt_exclusao.getDate() + periodo));
    alert(nova_data);
    alert(periodo);
    nova_data.setDate(nova_data.getDate() + periodo);
    alert(nova_data);

In the fiddle it worked, but on the page did not. The function on the page looks like this:

function montaDataSubstituicaoPrestador(dt_exclusao, periodo){

    var arrData = dt_exclusao.split('/');
    var novaData = new Date(arrData[2] + '-' + arrData[1] + '-' + arrData[0]);

    alert(arrData);
    alert(dt_exclusao);
    alert(periodo);
    alert(novaData);
    novaData.setDate(novaData.getDate() + periodo);
    alert(novaData);    
}

The big difference is that fiddle I put dt_exclusao on hand and the function I get. Date mounted on novaData looks like this: NaN . The arrData is correct, type: 26,11,2015, but the assembly gives error NaN .

    
asked by anonymous 26.11.2015 / 18:18

2 answers

11

The new Date() constructor with string must be in mm-dd-yyyy or mm/dd/yyyy format or you can still use constructor with integer new Date(yyyy, mm, dd)

Reference: link

In your case, you need to convert the string to one of these formats, you can do this using the split () , and then access the generated array values to use in the constructor you prefer.

  

Note : When using constructor with integers, the month starts with 0 , so I added it in the -1 constructor.

var dataExclusao = '26/11/2015';
var arrDataExclusao = dataExclusao.split('/');

var stringFormatada = arrDataExclusao[1] + '-' + arrDataExclusao[0] + '-' +
  arrDataExclusao[2];
var dataFormatada1 = new Date(stringFormatada);
var dataFormatada2 = new Date(arrDataExclusao[2], arrDataExclusao[1] - 1, arrDataExclusao[0]);

console.log('Data formatada 1: ' + dataFormatada1);
console.log('Data formatada 2: ' + dataFormatada2);

dataFormatada1.setDate(dataFormatada1.getDate() + 60);
dataFormatada2.setDate(dataFormatada2.getDate() + 90);

console.log('Data formatada + 60 dias: ' + dataFormatada1);
console.log('Data formatada + 90 dias: ' + dataFormatada2);
    
26.11.2015 / 19:03
9

The Date constructor (as well as the method Date.parse ) is very restricted in the accepted format . The format is the one you mentioned:

YYYY-MM-DDTHH:mm:ss.sssZ

Z refers to the UTC time zone. You have a browser that even accepts other formats, but as they are not mentioned in the specification >, it's best to use this one.

An alternative to this format is to pass the parts of the date as separate parameters:

var hoje = new Date(2015, 10, 26);
    
26.11.2015 / 19:00