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

202
Vistas
how to get year from datestring with javascript jquery

I have a td element in which is a date:

<td class="date-table">2021-09-02</td>

How can i get the year from that string?

Below does not work

var date = $(.date-table').text(); // read text inside td
var d = new Date(date);
var year = d.getFullYear();  
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Missing a opening quote in your first line of js.

(I also added proper table markup around your tr and an alert for showing the years value for the sake of completeness).

var date = $('.date-table').text(); // read text inside td
var d = new Date(date);
var year = d.getFullYear();
console.log(year);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td class="date-table">2021-09-02</td>
  </tr>
</table>

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