0

ok.. just a heads up.. I am new to web developing.. I am just trying to understand how everything works.. This is the part of code that doesn't work:

HTML

<div class="flags" id="CV"><h3>Experienta profesionala</h3>
<p><input type="date" id="angajare" value="2015-03-01" readonly hidden><span id="data_angajare"><script>dataAngajare()</script></span> - Prezent - <strong>Tehnician IT</strong></p>
<p><strong>KraftCom GmbH</strong>, Dorfstr. 17, 86869 Oberostendorf, Germania</p>
<p>Timp lucrat: <span id="timp_lucrat"><script>timpLucrat()</script></span></p>

The funtion dataAngajare() works and displays correctly.

function dataAngajare() {
const months = [
  'Januarie',
  'Februarie',
  'Martie',
  'Aprilie',
  'Mai',
  'Iunie',
  'Iulie',
  'August',
  'Septembrie',
  'Octombrie',
  'Noiembrie',
  'Decembrie'
];
var angajare = document.getElementById('angajare').value;
var dataAngajare = new Date(angajare);
var monthIndex = dataAngajare.getMonth();
var year = dataAngajare.getYear() + 1900;
var monthName = months[monthIndex];

document.getElementById('data_angajare').innerHTML = monthName + ' ' + year; 
}

The funtion timpLucrat() however does nothing at all. It doesn't display anything and I don't understand why.

funtion timpLucrat() {
var angajare = document.getElementById('angajare').value;
Date dataAngajare = new Date(angajare);
Date today = new Date();
var month_old = dataAngajare.getMonth();
var year_old = dataAngajare.getYear();
var current_month = today.getMonth();
var current_year = today.getYear();
if (current_month > month_old) {
    var month_diff = current_month - month_old;
    var year_diff = current_year - year_old;
} else if (current_month == month_old) {
    var month_diff = 0;
    var year_diff = current_year - year_old;
} else {
    var month_diff = current_month + 12 - month_old;
    var year_diff = current_year - year_old - 1;
}
document.getElementById('timp_lucrat').innerHTML = year_diff + ' ani si ' + month_diff + ' luni'; 
}

the functions are stored in 2 diferent .js files which are imported in the header element:

Can anyone spot what the problem is? I realy don't know how to proceed.. Thank you for your time!

Highlighted is the result of the funtion dataAngajare() and after "Timp lucrat: " I should have the result of the function timpLucrat() but nothing is shown.

  • Just [this](https://www.google.com/search?q=js+months+and+year+between+two+dates) google search returns a lot of results – Anurag Srivastava Apr 01 '20 at 15:07
  • Should I understand that everything I did is wrong? I understand there are other ways to do this but I want to know if my way could work and if so what I am doing wrong? Also I don't understand why one of the funtions work and the other one doesn't.. I am looking for advice regarding my solution.. not for other solutions.. Thank you – Paul Adrian Cristea Apr 01 '20 at 15:32
  • No, but the way to go about that is thinking in reverse. So your last statement in the code doesn't append anything to html. Think why. Add a `console.log()`before it, check the variables, and so on. That would shed some light on what you are doing incorrectly. The various linked examples serve to show you where you might be wrong, so don't take it as a sign of you not doing anything right. – Anurag Srivastava Apr 01 '20 at 15:35
  • actually writing "function" and not "funtion" helps! Thank you for your help :) Everything works now! – Paul Adrian Cristea Apr 01 '20 at 15:42

1 Answers1

0

actually writing "function" and not "funtion" helps! Thank you for your help :) Everything works now!

  • You should have seen a console error if this was the case `timpLucrat is not a function`. Did you inspect the console? – Anurag Srivastava Apr 01 '20 at 15:45
  • I haven't done that.. As I said, I am quite new to this and still trying to get the hang of everything.. But this is a very good tip.. I will take this into account for future errors.. Thank you! – Paul Adrian Cristea Apr 01 '20 at 15:49