Convert string to date

Asked

Viewed 14,461 times

8

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

That way it doesn’t 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 how it works, it means that it is the past format that lies 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);

The fiddle worked, but the page didn’t. The function on the page is 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 place in my hand the dt_exclusao and in the function I receive. Date mounted on novaData comes like this: NaN. The arrData is correct, type: 26,11,2015, but the setup gives error NaN.

  • 5

    No point in passing dd/mm/yyyy for new Date(). Have you found an alternative format that is accepted, why not use it?

  • 2

    @bfavaretto, the date already comes from another function that picks up from the bank in this format dd/mm/yyyyall my problem is that I’m not getting to turn from a format(string - dd/mm/yyyy) for the accepted. This is the reason for the post.

  • 1

    Break the value with split as suggested by Pedro

  • 4

    In your fiddle, there is no Date object, it won’t even work.

  • 1

    In reality this is another question, even for the same function. Here I ask regarding how to move from string to date, so it has not been answered yet.

  • 3

    No? Pedro Camara Junior explains exactly how to do.

  • 1

    Hadn’t seen the discussion here, it’s working now @pnet ?

  • 3

    But here’s the fiddle working: http://jsfiddle.net/ivanferrer/8hz20pkL/

Show 3 more comments

2 answers

12


The builder new Date() string should be in format mm-dd-yyyy or mm/dd/yyyy or you can also use the whole constructor new Date(yyyy, mm, dd)

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

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 values of the generated array to use in the constructor you prefer.

Note: When using integer constructor, the month starts at 0, so I added in the constructor -1.

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);

10

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

YYYY-MM-DDTHH:mm:ss.sssZ

The Z refers to the UTC time zone. It has browser that even accepts other formats, but like them are not mentioned in the specification, you better use that one.

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

var hoje = new Date(2015, 10, 26);

Browser other questions tagged

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