0


I am relative new in coding and recently i have to do some date manipulations with data entered in a form. I searched a lot and I haven't found the solution that works for me.
Here is the issue:

The user enters following data in datetime-local fields in HTML:
1) Year of entering College (date 1)
2) Year of graduation (date 2)
3) Birthday (date 3)

I need a JQuery or a JavaScript script that uses this dates and output following:
1) Year of admitting, based on the admittion date (I know there is a getFullYear function, but I couldn't use it right...) >>> result1
2) Age at graduation (date difference between Birthdate and date of Graduation. >>> result2

I tried with adding datepicker but somehow I got really awful looking calendar which was displayed over the boxes. I couldn't install datetimepicker...

Thank you in advance. Your help is appreciated.

Here is my HTML code.

<input id="date1" type="datetime-local"/>
<label for="date1">admittion in Colleage</label>
<br />
<input id="result1" type="text"  readonly="true" name="year" placeholder="=(getFullYear from date1)"/>  
<label for="result1">year</label>    
<br />


<input id="date2" type="datetime-local" />
<label for="date2">graduation</label> 
<br />

<input id="date3" type="date" name="born" />
<label for="date3">born</label>
<br />

<input id="result2" type=“text” name="age" readonly="true" placeholder="=DATEDIF(Date3, Date2, Year)"/></textarea>
<label for="result2">age</label> 
<br />
pap
  • 123
  • 1
  • 2
  • 14
  • what did you try ? – sheplu Oct 23 '17 at 20:37
  • Why do u need time for first two date fields, IMHO better use type='date'. – Kamal Oct 23 '17 at 21:02
  • @Kamal - for this particular case the time makes no sense, but I have another pair of date in my code, where the time matters. I thought to use a. Ode that works and just to change the “ids”. – pap Oct 23 '17 at 21:08
  • @sheplu my problem is that I don’t know how to manipulate the dates that are entered in the datetime-local inputs and to proceed those further... – pap Oct 23 '17 at 21:09

4 Answers4

2

You didn't include the js code which is more important for this question. I'll include a couple functions that should help.

To find a year from a date, I use the following function

function getYearFromDate(date){
  const yearFromDate = new Date();
  yearFromDate.setTime(date.getTime());
  yearFromDate.setFullYear(date.getFullYear() + 1);
  return yearFromDate;
}

console.log(getYearFromDate(new Date()));

And the graduation age copied mostly from Calculate age given the birth date in the format YYYYMMDD

function getYearsBetween(oldDate, newDate) {
    var age = newDate.getFullYear() - oldDate.getFullYear();
    var m = newDate.getMonth() - oldDate.getMonth();
    if (m < 0 || (m === 0 && newDate.getDate() < oldDate.getDate())) {
        age--;
    }
    return age;
}

Including moment.js is also an option, but it's a large library I like to avoid if I can.

EDIT I thought you wanted year + 1 day. Getting the full year from the date is even easier.

const date = new Date();

console.log(date.getFullYear());

Edit: All together now

function updateGradYear(event) {
  console.log(event.target.value);
  let date = new Date(event.target.value);
  document.querySelector('#result1').value = date.getFullYear();
}

function updateAge() {
  let gradDate = new Date(document.querySelector('#date2').value);
  console.log(document.querySelector('#date3').value);
  let birthdate = new Date(document.querySelector('#date3').value);
  document.querySelector('#result2').value = gradDate.getFullYear() - birthdate.getFullYear();
}



// set some default values
let today = new Date();
document.querySelector('#date1').value = today.toISOString().split('.')[0];
document.querySelector('#result1').value = today.getFullYear();
document.querySelector('#date2').value = today.toISOString().split('.')[0];

let birthdate = new Date(today.getTime);
birthdate.setFullYear(1984);
console.log(birthdate.toISOString().slice(0, 10).replace(/\//g, "-"));
document.querySelector('#date3').value = birthdate.toISOString().slice(0, 10).replace(/\//g, "-");

updateAge();
<input id="date1" type="datetime-local" onchange="updateGradYear(event)" />
<label for="date1">admittion in Colleage</label>
<br />
<input id="result1" type="number" readonly="true" name="year" placeholder="=(getFullYear from date1)" />
<label for="result1">year</label>
<br />


<input id="date2" type="datetime-local" onchange="updateAge()" />
<label for="date2">graduation</label>
<br />

<input id="date3" type="date" name="born" onchange="updateAge()" />
<label for="date3">born</label>
<br />

<input id="result2" type=“text” name="age" readonly="true" placeholder="=DATEDIF(Date3, Date2, Year)" /></textarea>
<label for="result2">age</label>
<br />
Amos47
  • 695
  • 6
  • 15
  • You can remove the line `yearFromDate.setTime(date.getTime())` if *yearFromDate* is initialised using `new Date(+date)`. ;-). There should also be a note about adding 1 year to 29 February, as some places may want 28 Feb and others 1 Mar (javascript does the latter). – RobG Oct 24 '17 at 01:48
  • Hi @Amos47. Thanks for the reply. I didn’t add a code, because mine didn’t make any sense. For some reason I can’t make your code work. I added the function name to a button with onclick=“function-name”. It doesn’t work. Furthermore I don’t understand how the function takes the dates from fields with id “date2” and “date3”. Besides I need to pick the date/time data from a “datetime” field (a drop down calendar), which is filled by the user. I need also all calculations to be done automatically after entering the dates. And the result must be displayed in the field with id “result2”. Thanks. – pap Oct 24 '17 at 20:12
  • I updated it to show how it can be done all together. – Amos47 Oct 24 '17 at 20:57
1

Let me know how this works for you! And as i said please enter all the fields while running this code!

$('#date1, #date2').on('blur',function(){
  var dc = document.querySelector('input[id="date1"]');
  var date = new Date(dc.value);
  var collegeadmission = date.getFullYear();
  $('#result1').val(date.getFullYear());
  
  dc = document.querySelector('input[id="date2"]');
  date = new Date(dc.value);
  var collegegrad = date.getFullYear();
  if(! isNaN(collegegrad))
   $('#result2').val(collegegrad - collegeadmission);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="date1" type="datetime-local"/>
<label for="date1">admittion in Colleage</label>
<br />
<input id="result1" type="text"  readonly="true" name="year" placeholder="=(getFullYear from date1)"/>  
<label for="result1">year</label>    
<br />


<input id="date2" type="datetime-local" />
<label for="date2">graduation</label> 
<br />

<input id="date3" type="date" name="born" />
<label for="date3">born</label>
<br />

<input id="result2" type=“text” name="age" readonly="true" placeholder="=DATEDIF(Date3, Date2, Year)"/></textarea>
<label for="result2">age</label> 
<br />
Kamal
  • 2,550
  • 1
  • 8
  • 13
  • HI @Kamal, thanks for the code! The first part works just fine (year of entering the college). The second part is unfortunately wrong (it should be date difference between BORN and GRADUATION). – pap Oct 24 '17 at 21:54
  • looks like you already got the answer... its on the same line. let me know if u still need some more input! – Kamal Oct 24 '17 at 23:12
  • The code above works for me. Still I would be happy if you submit your code too. After all you put a lot of efforts until now. Thanks and best regards. – pap Oct 26 '17 at 21:08
-1

check out this url:http://jsbin.com/vebiwogipu/edit?html,js,output

Make sure u fill all the dates and time properly as you discussed in your html.

Similarly you can attach the output on click or change event. and aso you can leverage getMonth() and getDay() in the code to get desired output.

Kamal
  • 2,550
  • 1
  • 8
  • 13
-1

enter image description here

Take a look at the label and may be alert, when you click the button, it shows year out of your first date field. As i said you need to add the logic for rest.

let me know if it makes sense, if not please add more information to make me understand!

Kamal
  • 2,550
  • 1
  • 8
  • 13
  • Post code, not images. Did you really need another answer, or could you have edited your other answer? – RobG Oct 23 '17 at 23:17
  • Sorry, it doesn’t work... @kamal, can you tell me how to select the data from this “date-time-local” field and to proceed them further for calculations? I also need to know how to add the result to field with an id “result2”. Thanks!!! – pap Oct 24 '17 at 20:14