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