2

Edit: I tried some of the suggestions at the answer below, but I still don't get the appropriate results. Any further help would be appreciated.

I have implemented a form in HTML that takes 2 dates as inputs and they are appended into two arrays. Then I calculate the difference between the 2 dates in months.

a) The form asks the user about his/her five year history and therefore the user should be able to input dates until he/she riches five years(60 months). For this issue, I tried the function below:

if (typeof startArray[index] !== 'null' && endArray[index] !== null) {
var result = difference_between(dateFrom, dateTo);
document.getElementById("txtResult").value = result
if(result <= 60) {
document.getElementById('datepicker1').reset();
document.getElementById('datepicker2').reset();
document.getElementById('txtResult').reset();
}
else{
alert("Go to the next section");
document.getElementById('btnSave').disable = true;
}

b) The form should be able to calculate the gap between the end date and the next start date and there should not be a gap of more than two months. For example, the form should be able to compare the date I ended school with the date I started uni and if it's bigger than 2 months, then I should thow an error message. Here is my function:

function GetDifference() {
var dateFrom = new Date(document.getElementById("datepicker1").value);
var dateTo = new Date(document.getElementById("datepicker2").value);
document.getElementById("txtResult").value = difference_between(dateFrom, dateTo);

//Is there a gap of more than 2 months?
if (endDate.length && (difference_between(endDate[endDate.length - 1], dateFrom)) > 2) {
    window.alert("There should not be a gap of more than two months in this section.");
}
}

here is the JSFIDDLE: http://jsfiddle.net/stelios13/KsuxV/111/

Thank you.

Stelios Voskos
  • 526
  • 5
  • 17
  • So you need to know how get the difference in months between two dates? Your question is very long .. maybe make a new one that is shorter with just the relevant code. – Halcyon Aug 30 '13 at 10:14
  • I got the difference in months. The thing now, is that I need to calculate the gap between the previous end date (INDEX 0 at the endArray and index 1 at the start array. Also I need to stop the input, when the user riches the 5 - year limit (60 months) – Stelios Voskos Aug 30 '13 at 10:18
  • Maybe this will be relevant here: http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript – Anderson Green Aug 30 '13 at 14:53
  • @AndersonGreen Thank you for your reply. This is what I have already done and the difference is calculated. However, the issue is that we have to calculate the gap between the end date and the next start date in the array. So I have to find a way to ignore the first date in the startDate[] array and calculate the gap. Also, I have to stop the input, when the user riches the five years. Any ideas? Thanks – Stelios Voskos Aug 30 '13 at 15:02
  • You should try and simplify your question. I've read it all and kind of got confused... The way it is right now I don't think you'll get an answer.. – Itay Aug 30 '13 at 16:08
  • @Itay Thanks for replying. I want to do 2 things: a) To stop the input when the user riches the 5 years and b) calculate the gap between the last end date and the next start date in the array. – Stelios Voskos Aug 30 '13 at 16:16
  • Are you aware that right now one can add the same date again and again? – Itay Aug 30 '13 at 16:22
  • No, I haven't noticed that. Why? – Stelios Voskos Aug 30 '13 at 16:27
  • Damn, this question is too long... – kumarharsh Aug 30 '13 at 16:40

2 Answers2

1

startDate.splice(0,1) deletes the first element in startDate, I have a feeling that's not what you want. I'd do the check like this:

function GetDifference() {
    var dateFrom = new Date(document.getElementById("datepicker1").value);
    var dateTo = new Date(document.getElementById("datepicker2").value);
    document.getElementById("txtResult").value = difference_between(dateFrom, dateTo);

    // Right now the things in endDate are strings so this won't work, but see below
    if(endDate.length && (difference_between(endDate[endDate.length - 1], dateFrom)) > 2){
        window.alert("There should not be a gap of more than two months in this section. Plsease fill in your details appropriately");
        document.getElementById("btnSave").disabled = true; 
    }
}

…First I make sure that endDate isn't empty, then I get the last date in endDate and compare it to dateFrom. Is that what you're looking for?

You can do something similar to check for a five-year range: First make sure that startDate and endDate are not empty, then compare the first date in startDate (startDate[0]) with the last date in endDate (endDate[endDate.length - 1]).


By the way, in JavaScript dates can convert to numbers (as the number of milliseconds from an arbitrary start point, January 1, 1970):

// The unary `+` operator converts its operand to a number:
console.log(+new Date()); // Just now, logged 1377880989982

This is great for comparing them:

// How many milliseconds in a month?
var month = 1000 * 60 * 60 * 24 * 365.25 / 12;
// Subtraction also converts its operands to numbers:
console.log(
    Math.round((new Date('2013-06-01') - new Date('2013-01-01')) / month)
); // Logs 4.96

So, there are about five months between January 1st and June 1st. If, in your case, you want to be a little more human (and make sure the month number is no more than two apart), then the way you're comparing them is totally fine.


One other little suggestion: If you're going to be comparing these dates later, you might want to store Dates in the arrays, not strings. And, .push() is a more common way to add something to an array than assigning to arr[arr.length]:

// I would change the beginning of insert() to something like this:
function insert() {
    var newStartDate = new Date(document.getElementById('datepicker1').value);
    var newEndDate = new Date(document.getElementById('datepicker2').value);
    startDate.push(newStartDate);
    endDate.push(newEndDate);

    // …
}
s4y
  • 50,525
  • 12
  • 70
  • 98
  • thank you very much for your advice. I'll try to embed your solution into my code. However, I still don't understand the first piece of code. The point is to calculate the difference between the end date(say the date you ended school) and the date you started university. So, I don't know if the first element in the startDate array can be useful. Imagine that the first date in the array, is the date that I started school. Then I don't need that If I want to calculate the difference of the date I ended school with the date I started uni – Stelios Voskos Aug 31 '13 at 02:35
  • Hey @AndreUk13. I don't use `startDate` at all in my first piece of code, what do you mean? Above it, I mentioned `startDate.splice(0,1)`, which you have in your code and probably shouldn't (since it deletes the first item in `startDate`). I mentioned `startDate[0]` in relation to your five-year history: comparing `startDate[0]` with `endDate[endDate.length - 1]` would be a good way to get the total time span of everything that's been entered so far. – s4y Aug 31 '13 at 06:58
  • @AndreUk13 Just to make sure we're talking about the same thing, my first piece of code compares `endDate[endDate.length - 1]` with `dateFrom`, right? – s4y Aug 31 '13 at 06:59
  • @Sindnicious Yeah sure, I see the point now! So for the 5 year history, I can put a for loop like: for(k=0; k < endDate.length; k++) and then the condition that you mentioned above. Thanks again – Stelios Voskos Aug 31 '13 at 09:06
  • Yeah sure, I see the point now! So for the 5 year history, I can put a for loop like: for(k=0; k < endDate.length; k++) and then the condition that you mentioned above. Thanks again – Stelios Voskos Aug 31 '13 at 13:02
  • @AndreUk13 Do you need a loop? I thought comparing the first start date with the last end date would do the trick. – s4y Sep 04 '13 at 05:18
  • Apologies my friend for the late reply. I left this issue away for a couple of days because I couldn't find a solution. I tried to embed your suggestions but it doesn't seem to work. If you can provide me an example on a jsfiddle with the code or something that would be appreciated. Here is my most recent jsfiddle http://jsfiddle.net/KsuxV/133/. Thanks for the help anyway. – Stelios Voskos Sep 17 '13 at 16:21
  • @AndreUk13 What doesn't work? It looks like it works to me. The only thing it doesn't have is the five-year check. If that's what you were having trouble with, try this: http://jsfiddle.net/KsuxV/134/. Look at lines 26-29. – s4y Sep 17 '13 at 18:28
  • Hi Sidnicious. Thanks for the jsfiddle. The five year history was working and it seems that I copied the wrong jsfiddle. Sorry for that. The thing that was not working for me was the 2 month gap. So I'm entering for example at the end date 10/09/2011 and at the next start date 02/09/2012. This should through an error, because the gap is bigger than 2 months... Do you get the relevant alert message on the screen? I mean that: window.alert("There should not be a gap of more than two months in this section. Please fill in your details appropriately");. Thanks – Stelios Voskos Sep 17 '13 at 19:13
  • Yes, I do. I enter the first pair of dates (end date "10/09/2011") and click "save", then enter a second pair of dates (start date "02/09/2012") and click "get difference". The alert pops right up. – s4y Sep 17 '13 at 19:52
  • Well, I hope to work for the rest of the people of my company and not for me :) OK thank you my friend for all your help and support. All the best! – Stelios Voskos Sep 18 '13 at 08:10
0

Maybe moment.js can be useful for you. It's a js library for working with dates, and it also have a diff function.