How to work with Javascript split with dates?

Asked

Viewed 79 times

0

Look at the picture;

inserir a descrição da imagem aqui

The Alert that appears is this ;

inserir a descrição da imagem aqui

The javascript code is this;

var resultado = $('#idPeriodoInicio').val().split("/");
    alert("o valor do split   " + resultado);

How do I get him to show up like this 2026?

The original date value is this!

inserir a descrição da imagem aqui

  • You want only 2026 to appear?

  • I don’t understand exactly what you want, need to appear only the year or 4/2026?

  • @That’s right, I just wanted you to show up 2026

  • Hmm got it. you could use something like this if it was already in date: var dt = new Date(); Document.write("getYear() : " + dt.getFullYear()); But we need to first know how to turn date by passing parameter to date() from the field data to then use getFullYear()

1 answer

2


The splitreturns an array, just take the second position of the array as below:

var resultado = $('#idPeriodoInicio').val().split("/");
alert("o valor do split   " + resultado[1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="idPeriodoInicio" value="04/2026" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.