Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
counter with leading zeros not returning the right result

Whenever I click, I count from 01 to 10, 01 all the way to 09 works but when it reaches 10 it messes up the number.

I have a function that receives a year variable which is the last 2 numbers of a year ex: 2022, the year for that in my case would be 22. I want to then count from 01 to 10 without removing the leading zeros. My code works once it's larger than 10 but whenever it's at 9 and then hits 10, the year gets a 0 added. This is an example of what the result would look like : 2008, 2009, 20010. I'm not sure why this happens because I'm only going until 9 and then checking if it's equal to 10...

             if (parseInt(strYear) < 10) {
                
                
                location.href = 'calendar.htm?ddlDay=' + numDay + '&ddlMonth=' + numMonth +
                        '&ddlCentury=' + numCentury + '&ddlYear=0' + (parseInt(strYear) + 1)

                
            }
            else if (parseInt(strYear) == 10) {
                
                location.href = 'calendar.htm?ddlDay=' + numDay + '&ddlMonth=' + numMonth +
                    '&ddlCentury=' + numCentury + '&ddlYear=' + parseInt(strYear)
            }
            else if (parseInt(strYear) > 10) {
                
                location.href = 'calendar.htm?ddlDay=' + numDay + '&ddlMonth=' + numMonth +
                    '&ddlCentury=' + numCentury + '&ddlYear=' + (parseInt(strYear) + 1)

            }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you can do that without those "if" conditions. Check the code below if that produces your desired output...

const numDay = 20; const numMonth = 12;
const numCentury = 20; const strYear = 22;

location.href = 'calendar.htm?ddlDay=' + numDay + '&ddlMonth=' + numMonth + '&ddlCentury=' + numCentury + '&ddlYear=' + numCentury + strYear.toString().padStart(2, '0');

Output: calendar.htm?ddlDay=20&ddlMonth=12&ddlCentury=20&ddlYear=2022

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda