How to get the day with two houses in JS

Asked

Viewed 370 times

3

I have a date form, and I need to generate its 'min' and 'max' according to the current date. I made a function that calculates this date and arrow it in the form. The problem is that the value of the day is coming with 1 house and in html I need two houses. Follow 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, there is some way to format it?

3 answers

5


There are better ways to do this. Follow 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 method toISOString() will format a date object in a date style YYYY-MM-DDTHH-mm-sssZ. As you only want the date, the split('T') will break the string in T, and the [0] will only take the date string ([1] will have the time string in case you need something).

For more information:

Date.prototype.toISOString()

3

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

Its 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'

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

Browser other questions tagged

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