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

162
Vistas
span textContent split returns undefined in javascript funtion

I am trying to help a friend out with the following javascript. We both are new to javascript.

We are trying to convert the date string to a different format. Based on internet search we understand that the date function expects the input string to be Date(year, month, day) format. To achieve that we need to parse the input string and send it in the expected format. We have not got a clue why string split on span tag's textcontent (that contains the date string) is not working.

<script type="text/javascript">
function ready(callback){

    if (document.readyState!='loading') callback();
    else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
    else document.attachEvent('onreadystatechange', function(){
        if (document.readyState=='complete') callback();
    });
}
    window.onload = ready(function() {
        var dateString = document.getElementById("dateFormatter").textContent.trim();
        var sMonth = dateString.split("/")[0];
        var sDay = dateString.split("/")[1];
        var sYear = dateString.split("/")[2];
        document.getElementById("dateFormatter").textContent=sMonth;
    });
</script>

The html has the following span tag.

<span id="dateFormatter">26/06/1993</span>

sMonth returns 26/06/1993, whereas sDay and sYear returns undefined.

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

0

The string from your div is invalid format if you want to convert to a datestring. A valid string would be: yyyy-mm-dd. If you have already a dateobject you can use the function to format the date.

Like that:

Update

callback();
window.addEventListener('load', function () {           
      var dateString = document.getElementById("dateFormatter").textContent.trim();
      console.log(dateString)
      let dateArr = dateString.split("/");
      var sDay = dateArr[0];
      var sMonth = dateArr[1];
      var sYear = dateArr[2];
      let newDateString = (sYear + '-' +  sMonth + '-' + sDay);
      let d = new Date(newDateString);
      console.log(d);
      document.getElementById("dateFormatter").textContent=d.toLocaleString();
});
<span id="dateFormatter">26/06/1993</span>

old snippet

function ready(callback) {

    if (document.readyState!='loading') callback();
    else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
    else document.attachEvent('onreadystatechange', function(){
        if (document.readyState=='complete') callback();
    });
}

    window.onload = ready(function() {
      var dateString = document.getElementById("dateFormatter").textContent.trim();
      console.log(dateString)
      let dateArr = dateString.split("/");
      var sDay = dateArr[0];
      var sMonth = dateArr[1];
      var sYear = dateArr[2];
      let newDateString = (sYear + '-' +  sMonth + '-' + sDay);
      let d = new Date(newDateString);
      console.log(d);
      document.getElementById("dateFormatter").textContent=d;
    });
<span id="dateFormatter">26/06/1993</span>

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is that you're setting the textContent of the span to only show the value of sMonth and when that happens, the span will only have "26" as its body (whitch btw is wrong if you write dates like so) - and actually should not be "26/06/1993" if the code is right.

Also, if you want to run the script multiple times, try setting the span's textContent to sDay+"/"+sMonth+"/"+sYear so it's the same formatting you expect the script to work with. The code you provided sets the span's value to "26" then if you run it again, it tries to split "26" along "/"-s, which of course ends up being incorrect.

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