1
I’m trying to figure out the time difference between two dates using the Moment.js
, but I need to observe some special conditions:
Case 1: If you enter a day in a month, it already counts as "a whole month". By example: of the day
31/09/2015
until01/10/2015
= 2 months. In another example, of31/07/2015
until16/10/2015
= 4 months.
In this last example, by Moment.js
the result is 2 months (77 days). It follows a fiddle what forkei of posted by @Orion in the comments.
Update 2: I think I really got it now resolve this part a gambiarra (to using the moment.js
and the split
, so I call twice the same values... gambiarra this right??? ) follows the fiddle commented (I will not post the code here for now to question not get bigger yet, then put as answer if it does not appear a better/simpler solution)... /update
This above is a calculation, and this below is another, unrelated to each other (one is not the condition of the other, they are independent, although starting from the same principle - put a minimum limit of days to be considered a month).
Case 2: To count as a whole month you must have more than 15 days on month. For example: of the day
16/09/2015
until14/10/2015
= 1 month (because only the month enters 09, since September has 31 days and we always consider the start day and end day. If it were up to 15/10/2015 it would be two months). In another example, of31/07/2015
until13/10/2015
=2 meses
(as they did not reach 15 days, neither July nor October enter).
In PHP I managed with a huge gambiarra, breaking the dates with explode
and making hundreds if's
to differentiate months of 30, 31 and 28 days. As in this case the period was not more than one year, so it worked. But here in this case it has to be in the client (in javascript or jquery), and the difference can be several years.
What I’ve done so far is this:
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 10/10/2010
and 09/10/2011
is 11
, and only if you complete a whole month becomes 12
(on 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)? Can be with or without library (the moment.js
or other). Thanks in advance.
I didn’t quite understand these rules that you put, but I created a link here http://jsfiddle.net/71adur8e/ so that you have a fresh head to make the logic.
– Paulo
Thanks @Orion, I forgot to create the fiddle! So, the logic is simple, but as the cases were together in a single quote I think it was really confusing. They are two different cases: in one calculation, I need to consider a month even if I only have one day. In the other, I need to have at least 15 days. I’ll edit the question. Thanks anyway.
– gustavox
@ Orion, I think I found a way to solve the first case (more or less the same way I did in php) using the
split
. Check it out > http://jsfiddle.net/gustavox/s28Lp779/11/. I haven’t tested it extensively yet, but from the tests here I think it’s okay. Now the second part needs to consider the months of 31 days and such (to see if it was 15), so I see here now...– gustavox
Nah, forget it, it’s not right yet... I’ll have to add a few more
ifs
... rsrs– gustavox