How to add a specific year to the current year in Javascript?

Asked

Viewed 77 times

-2

I want to create a javascript function that adds the year 1993 + current ano_and that inserts the result into my <span class="title_25anos" id="calculo_ano"></span></h1>

And this is my code.

<script>

    var ano_inicio = 1993;
    var ano_atual = getFullYear();

    var soma_ano = ano_inicio + ano_atual;

    document.getElementById("calculo_ano").innerHTML = "Há " . soma_ano . "anos com você";

</script>
  • What value you expect returns?

  • 2019 - 1993 = 26 But this value needs to be automatic.

  • There is another problem 22/05/1993 11/02/2019 return 26, being difference of 25

  • Year difference value will be exact or approximate?

  • Will be approximated

  • document.getElementById("calculo_ano").innerHTML = "Há " . soma_ano . "anos com você"; Why do you have these points? This is not php, but javascript.

Show 1 more comment

1 answer

2


Hello.

To find difference of seconds, years,.

You calculate vf - vi.

vf = Final value.

vi = Initial value.

Solution:

<script>

    var ano_inicio = 1993;
    var data = new Date();
    var ano_atual = +data.getFullYear();

    var soma_ano = Math.abs(ano_atual - ano_inicio);
// return 26

    document.getElementById("calculo_ano").innerHTML = "Há " + soma_ano + "anos com você";

</script>

I hope I helped. Good afternoon!

  • But because the result is "26 years with you"

  • Now what? It worked?

  • 1

    Show!!! Thank you very much!

Browser other questions tagged

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