How to get the day with two houses in JS [duplicate]

3

I have a form of type date, and I need to generate the 'min' and 'max' it according to the current date. I made a function that calculates that date and arrow it in the form. The problem is that the value of the day is coming with 1 home and in html I need two homes. Here is the function:

function getDataMinima(){
        var dataMinima = new Date();
        dataMinima.setFullYear(dataMinima.getFullYear()-130);
        var dataForm = dataMinima.getFullYear() + '-' + dataMinima.getMonth() + '-' + dataMinima.getDate();
        console.log("entrou na função minima, data calculada:"+dataForm);

        var dataMaxima = new Date();
        dataMaxima.setFullYear(dataMaxima.getFullYear()-5);
        var dataMaximaForm = dataMaxima.getFullYear() + '-' + dataMaxima.getMonth() + '-' +dataMaxima.getDate();
        console.log("Entrou na funcao maxima, data calculada:"+dataMaximaForm);

        $("#campodata").attr({
             "min" : dataForm
        });
    }

My problem is that when this value is passed to html, I get: '1888-11-3', and '3' instead of '03' causes the rule to be ignored, is there any way to format this?

    
asked by anonymous 03.12.2018 / 17:19

3 answers

5

There are better ways to do this. Here is one of them:

function getDataMinima(){
    var dataMinima = new Date();
    dataMinima.setFullYear(dataMinima.getFullYear()-130);
    console.log("entrou na função minima, data calculada:"+dataMinima.toISOString().split('T')[0]);
    
    var dataMaxima = new Date();
    dataMaxima.setFullYear(dataMaxima.getFullYear()-5);
    console.log("Entrou na funcao maxima, data calculada:"+dataMaxima.toISOString().split('T')[0]);
    
    $("#campodata").attr({
         "min" : dataMinima.toISOString().split('T')[0]
    });
}

Explanation:

The toISOString() method will format a date object on a YYYY-MM-DDTHH-mm-sssZ style date. Since you only want the date, split('T') will break the string in T, and [0] will only get the date string ( [1] will have the time string, if you need something). >

For more information:

Date.prototype.toISOString ()

    
03.12.2018 / 18:18
3

You can also use the String.padStart() method. to fill in the number with leading zeros.

Your syntax is:

minha_string.padStart(tamanho)
// ou
minha_string.padStart(tamanho, string_de_preenchimento)

Where string_de_preenchimento is, by default, space ( ).

Example:

let numero = "3";

console.log(numero.padStart(2));      // ' 3'
console.log(numero.padStart(2, '0')); // '03'
console.log(numero.padStart(4, '0')); // '0003'
console.log(numero.padStart(4, 'X')); // 'XXX3'
    
03.12.2018 / 18:51
1

My suggestion is to create an auxiliary function to return the date in the format you want.

function getDataFormatada(data) {
    return data.getFullYear() + '-'
        + ('0' + (data.getMonth() + 1)).slice(-2) + '-'
        + ('0' + data.getDate()).slice(-2);
}

function getDataMinima(){
    var dataMinima = new Date();
    dataMinima.setFullYear(dataMinima.getFullYear()-130);
    var dataFormatada = getDataFormatada(dataMinima);
    console.log("entrou na função minima, data calculada:"+ dataFormatada);

    var dataMaxima = new Date();
    dataMaxima.setFullYear(dataMaxima.getFullYear()-5);
    console.log("Entrou na funcao maxima, data calculada:"+ getDataFormatada(dataMaxima));

    $("#campodata").attr({
         "min" : dataFormatada
    });
}
    
03.12.2018 / 18:56