Preparation of a Data string for insert into MSQL database

Hello everyone
I ran into a small problem, currently solved, but I would like to understand.
Preparing a string to introduce a date in the DB I need to have the fields month, hour, minutes and seconds always expressed with two characters.
Unfortunately, however, it seems with ". Length" to fail to check the string length, why?

I enclose the code I'm using below.
Surely it will be nonsense but I have to understand to avoid making stupid mistakes again.

The date in the query is now correct :
{"topic":"QueeryInsert","_msgid":"4860460f.55db48","query":"INSERT INTO Tb_DB (Data, Linea, NScheda, NRiga, QtaAv)\nVALUES ('2019-11-29T10:01:51.000Z', 7, 0, 0, 594);"}

CODE

// Funzione per il trattamento della Data e conversione 
// in stringa data completa di ora e fuso per insert nel DataBase
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear(),
        HH = d.getHours(),
        MM = d.getMinutes(),
        SS = d.getSeconds();
        

    MM = MM.toString();
    SS = SS.toString();
    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    // --------------------------------------------------------
    //       On HH MM and SS length don't work 
    // --------------------------------------------------------

    //if (HH.length < 2) HH = '0' + HH;
    //if (MM.length < 2) MM = '0' + MM;
    //if (SS.lenght < 2) SS = '0' + SS;
    // --------------------------------------------------------
    //       On HH MM and SS the comparison by value works !! 
    // --------------------------------------------------------
    if (SS < 10) SS = '0' + SS;
    if (MM < 10) MM = '0' + MM;
    if (HH < 10) HH = '0' + HH;
    
    /*
    if (MM == '0') MM = '0' + MM;
    if (MM == '1') MM = '0' + MM;
    if (MM == '2') MM = '0' + MM;
    if (MM == '3') MM = '0' + MM;
    if (MM == '4') MM = '0' + MM;
    if (MM == '5') MM = '0' + MM;
    if (MM == '6') MM = '0' + MM;
    if (MM == '7') MM = '0' + MM;
    if (MM == '8') MM = '0' + MM;
    if (MM == '9') MM = '0' + MM;
    
    if (HH == '0') HH = '0' + HH;
    if (HH == '1') HH = '0' + HH;
    if (HH == '2') HH = '0' + HH;
    if (HH == '3') HH = '0' + HH;
    if (HH == '4') HH = '0' + HH;
    if (HH == '5') HH = '0' + HH;
    if (HH == '6') HH = '0' + HH;
    if (HH == '7') HH = '0' + HH;
    if (HH == '8') HH = '0' + HH;
    if (HH == '9') HH = '0' + HH;
    
    if (SS == '0') SS = '0' + SS;
    if (SS == '1') SS = '0' + SS;
    if (SS == '2') SS = '0' + SS;
    if (SS == '3') SS = '0' + SS;
    if (SS == '4') SS = '0' + SS;
    if (SS == '5') SS = '0' + SS;
    if (SS == '6') SS = '0' + SS;
    if (SS == '7') SS = '0' + SS;
    if (SS == '8') SS = '0' + SS;
    if (SS == '9') SS = '0' + SS;
    */
    
    var D1 = '\'' + year + '-' + month + '-' + day + 'T' + HH + ':' + MM + ':' + SS + '.000Z' + '\'';  

    return D1;
}

var tempString =formatDate(Date().toString());

// Verifica se esiste la variabile globale
// Se non esiste la crea con valore pari a 0

var DateH=flow.get('DateH') || 0;

flow.set('DateH',tempString);


msg.payload=tempString;

return msg;

Thanks a lot to everyone

Have a look at the JavaScript padStart function:

Thanks so much