Difference between dates in javascript months

Asked

Viewed 1,816 times

1

People want to have the difference of months between two dates. However I’m missing something. I want to enter the dates in the inputs and the result appear in another input.

var DateDiff = {

inMonths: function(d1, d2) {
    var d1Y = d1.getFullYear();
    var d2Y = d2.getFullYear();
    var d1M = d1.getMonth();
    var d2M = d2.getMonth();

    return (d2M+12*d2Y)-(d1M+12*d1Y);
}}
var dataInicio= document.getElementById("dataInicio");
var dataFinal = document.getElementById("dataFinal");
document.write("<br />Numero de <b>months</b> since "+dataInicio+": "+DateDiff.inMonths(dataInicio, dataFinal));
  • Do you have any examples? I adptei this because I don’t know javascript. You can help me?

  • I didn’t even see it was javascript malz ae... peri

  • 1

    http://answall.com/questions/13046/diferen%C3%A7a-entre-datas do @Bacco

  • i did, ae ...?

  • Not yet. I tried to follow the example didn’t work. :(

  • peri, tho see something here

  • Tabom. I’ll wait. Thanks for your help.

  • @ Magichat nothing. As soon as you find me let me know.

Show 3 more comments

2 answers

2


See if that’s it... The accepted format is with bars : yyyy/mm/dd or dd/mm/yyyy or commas yyyy,mm,dd

<input type="date" id="dt1">Data 1</br>
<input type="date" id="dt2">Data 2</br>
<input type="text" id="result">Resultado</br>
<input type="submit" value="calcular" onclick="calcular();">
<script>
function calcular(){
var dt1 = document.getElementById("dt1").value; 
var dt2 = document.getElementById("dt2").value; 

var data1 = new Date(dt1); 
var data2 = new Date(new Date(dt2));
var total = (data2.getFullYear() - data1.getFullYear())*12 + (data2.getMonth() - data1.getMonth());
document.getElementById("result").value = total;
}
</script>

Anything comments that adjusts agent.

If any of the answers, if valid, could validate, in the green icon, below the evaluation arrows.. vlw;)

  • worked perfectly. Thanks man.

  • I’m with a new post. Can you help me? Please. http://answall.com/questions/141073/carregar-valor-em-input-text-depois-de-selecionar-valor-em-select-codeigniter

0

You need to instantiate dates in javascript pattern.

var DateDiff = {

inMonths: function(d1, d2) {
d1 = new Date(d1);
d2 = new Date(d2);
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();

return (d2M+12*d2Y)-(d1M+12*d1Y);
}}
var dataInicio= document.getElementById("dataInicio").value;
var dataFinal = document.getElementById("dataFinal").value;
document.write("<br />Numero de <b>months</b> since "+dataInicio+": "+DateDiff.inMonths(dataInicio, dataFinal));

Your date needs to be in a pattern accepted by javascript "yyyy-mm-dd" or "mm/dd/yyyy" for example, to use the new Date().

  • you have a model or input?

  • When you retrieve the date in the input which format it comes from ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.