jquery日期和时间计算firefox NaN

时间:2013-05-07 20:16:34

标签: javascript jquery datetime calendar

我正在尝试计算某个日期之间的天数,我正在使用日期和时间。在Chrome上运行正常,但在Firefox上给我“NaN”,还没有测试它,但我认为它会是相同的结果。                

            <form action="" enctype="multipart/form-data" id="userForm" method="post">
            <table border="0">
            <tbody>            
              <tr class="rsform-block rsform-block-paemimodata">
                <td>Delivery Date (*)</td>
                <td><input type="text" class="datepicker rsform-input-box hasDatepicker" id="paemimodata" name="form[paemimodata]" size="20" value="2013-05-15"></td>

              </tr>
              <tr class="">
                <td>Time (Hours) (*)</td>
                <td><select class="rsform-select-box" id="paemimolaikas" name=""><option value="01">01</option>
                    <option value="02">02</option>
                    <option value="03">03</option>
                    <option value="04">04</option>
                    <option value="05">05</option>
                    <option value="06">06</option>
                    <option value="07">07</option>
                    <option value="08">08</option>
                    <option value="09">09</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    <option value="13">13</option>
                    <option value="14">14</option>
                    <option value="15">15</option>
                    <option value="16">16</option>
                    <option value="17">17</option>
                    <option value="18">18</option>
                    <option value="19">19</option>
                    <option value="20">20</option>
                    <option value="21">21</option>
                    <option value="22">22</option>
                    <option value="23">23</option>
                    <option value="24">24</option>
                  </select>
        </td>
              </tr>  
              <tr class="">
                <td>Time ( minutes )</td>
                <td><select class="rsform-select-box" id="paemimolaikasmin" name="">
                    <option value="05">05</option>
                    <option value="10">10</option>
                    <option value="15">15</option>
                    <option value="20">20</option>
                    <option value="25">25</option>
                    <option value="30">30</option>
                    <option value="35">35</option>
                    <option value="40">40</option>
                    <option value="45">45</option>
                    <option value="50">50</option>
                    <option value="55">55</option>
                    <option value="00">00</option>
                  </select>
               </td>

                    退货日期()                                                                                                       时间(小时)()                                              01                         02                         03                         04                         05                         06                         07                         08                         09                         10                         11                         12                         13                         14                         15                         16                         17                         18                         19                         20                         21                         22                         23                         24                                                                                                       时间(分钟)                                              05                         10                         15                         20                         25                         三十                         35                         40                         45                         50                         55                         00                                                               
                               

        </form>
            </xmp>

Java脚本

                <pre>


            jQuery(function ($) {

            function coundays () {


            var starter = $('#paemimodata').val();

            var starterhour = $('#paemimolaikas').val();
            var startermin = $('#paemimolaikasmin').val();


            var ender =  $('#grazinimodata').val();

            var enderhour = $('#grazinimolaikas').val();
            var endermin = $('#grazinimolaikasmin').val();

            var fullstarter = starter + ' ' +  starterhour + ':' + startermin + ':00';

            var fullender = ender + ' ' + enderhour + ':' + endermin + ':00';

                var start = new Date(fullstarter);
                var end = new Date(fullender);
            var diff = new Date(end - start);
            var days = diff/1000/60/60/24;


            alert(days);

            };


             coundays ();


               $('#userForm').on('change', function() {
                    coundays ();

              });






            });


                </pre>

Js fidlle

http://jsfiddle.net/dezignas/j2HGw/13/

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

确保将字符串传递给符合ISO 8601标准的新日期,例如“2013-04-05T14:30”。那么这适用于所有现代浏览器。在你的情况下,我认为日期和时间之间缺少一个T.

答案 1 :(得分:0)

日期应符合RFC2822标准,如Mozilla文档(https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse

所述

将日期分配更改为:

var fullstarter = starter + 'T' +  starterhour + ':' + startermin + ':00';
var fullender = ender + 'T' + enderhour + ':' + endermin + ':00';