0

Sorry if Duplicate But could not Find anything Similar that can help me.

I am using datetime picker

And I am trying to calculate a date range based on Day + time

Till now I came with this In JS

$('#calculate').click(function(event){
  event.preventDefault();
  var darrival = $('#darrival').val();
  var rdate = $('#rdate').val();
  var start = new Date(darrival);
  var end= new Date(rdate);
  var finaltime=(end-start)/1000/60/60;
  alert(finaltime);
});

But I am getting NaN. I made

console.log(start)

And it show up "Invalid Date"

The value that I am taking(var darrival = $('#darrival').val();) Look like: 26-08-2013 01:08

I guess new Date doesnt count the time can anybody suggest something? thank you forrward.

SergkeiM
  • 3,934
  • 6
  • 36
  • 69
  • http://stackoverflow.com/questions/13903897/javascript-return-number-of-days-hours-minutes-seconds-between-two-dates – Sheetal Aug 26 '13 at 12:23

2 Answers2

2

I think you need to change more then one thing in your code ..

1) you need specify language and format while building datetimepicker

2) you need to get data using datpicker's api not using the textbox

pls see my below example this might help you

<body>

    <div class="well">

      <div id="rdate" class="input-append date">
        <input data-format="dd-MM-yyyy hh:mm:ss" type="text" id="date1"></input>
        <span class="add-on">
          <i data-time-icon="icon-time" data-date-icon="icon-calendar">
          </i>
        </span>
      </div>


        <div id="darrival" class="input-append">
            <input data-format="dd-MM-yyyy hh:mm:ss" type="text"  id="date2"></input>
            <span class="add-on">
                <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                </i>
            </span>
        </div>



    </div>

    <input type='button' name='calculate' id='calculate' value='click this' onclick='fn1()'>

    <script type="text/javascript">
      $(function() {
        $('#darrival').datetimepicker({
            format: 'dd/MM/yyyy hh:mm:ss',
            language: 'pt-BR'
        });

        $('#rdate').datetimepicker({
            format: 'dd/MM/yyyy hh:mm:ss',
            language: 'pt-BR'
        });


      });
    </script>

    <script type="text/javascript">

        $('#calculate').click(function(event){

          event.preventDefault();
          var picker1 = $('#darrival').data('datetimepicker');
          var picker2 = $('#rdate').data('datetimepicker');
          var start = new Date(picker1.getDate());
          var end= new Date(picker2.getDate());
          var finaltime=(end-start)/1000/60/60;
          alert(finaltime);

        });
    </script>

</body>
Dhaval
  • 2,801
  • 20
  • 39
  • Using ur example I was coming `TypeError: Cannot call method 'getDate' of undefined ` Error so I did it as following: var darrival = $('#darrival').val(); var rdate = $('#rdate').val(); var start= new Date(darrival); var end= new Date(rdate); var finaltime=(end - start) / 1000 / 60 / 60; alert(finaltime); **Fixed changed data format from:26-08-2013 01:08 2013-08-26 01:08** – SergkeiM Aug 26 '13 at 13:09
1

You can use moment.js difference method to calculate the date range.

Jake Lin
  • 11,146
  • 6
  • 29
  • 40