I'm trying to figure out the time difference between two dates using Moment.js
, but I need to look at some special conditions:
Case 1 : If you enter a day in a month, it counts as "a full month." Per example: from day
31/09/2015
to01/10/2015
= 2 months . In another example, from% with% to% with% = 4 months .
In this last example, by% w / w the result is 2 months (77 days). Here's a fiddle that forkei do posted by @Orion in comments.
Update 2: I think I've been able to solve this part a bit too much (to using 31/07/2015
and 16/10/2015
, then I call twice the same values ... gambiarra this né ???) follow the fiddle commented (I will not post the code here for now for the question does not get bigger yet, then post as an answer if a better / simpler solution does not appear) ... / update
This above is a calculation, and this below is another, unrelated (one is not a condition of the other, they are independent, although starting from the same principle - put a minimum limit of days to be considered a month) / p>
Case 2 : To count as a whole month you must have more than 15 days in the month. For example: from day
Moment.js
tomoment.js
= 1 month (because only month enters 09, since September has 31 days and we always consider the start day and end day. If it were until 10/15/2015 it would be two months). In another example, fromsplit
to16/09/2015
=14/10/2015
(as they did not reach 15 days, neither july nor october enter).
In PHP I got a lot of trouble, breaking the dates with 31/07/2015
and making 13/10/2015
to differentiate months of 30, 31 and 28 days. As in this case the period was not more than a year, then it worked. But here it has to be on the client ( javascript or < jquery "), and the difference can take several years.
What I've done so far was the following:
function result(e) // pega os dados do form
{
e.preventDefault();
var a = document.getElementById("data1").value; // INÍCIO
var b = document.getElementById("data2").value; // FIM
// DIFERENÇA EM DIAS DIAS DO DIA INICIAL X FINAL
var checkin = moment(a, 'DD-MM-YYYY');
var checkout = moment(b, 'DD-MM-YYYY');
var meses = checkout.diff(checkin, 'months');
if (meses > 0) {
$('#meses').html(meses);
}
}
The output here, with dates between 2 meses
and explode
is if's
, and only if it completes an entire month it becomes 10/10/2010
(as of 10/10/2011).
How can I solve this to achieve both logics (with only one day, or at least 15), considering the "real" months (not the calendar month - 30 days)? It can be with or without a library ( 09/10/2011
or other). Thanks in advance.