zoukankan      html  css  js  c++  java
  • 用JS计算时间差

    代码如下:

            function tbEndTime_OnBlur() {
                var stime = document.getElementById('<%= tbStartTime.ClientID %>').value.split("-");
                var etime = document.getElementById('<%= tbEndTime.ClientID %>').value.split("-");
                var st = new Date(stime[0], stime[1] - 1, stime[2]);
                var et = new Date(etime[0], etime[1] - 1, etime[2]);
                if (stime.length == 3 && etime.length == 3) {
                    var days = ((et - st) / (1000 * 60 * 60 * 24)) + 1;
                    if (days > 0) {
                        document.getElementById('<%= lbTotal.ClientID %>').innerText = days;
                    } else {
                        document.getElementById('<%= lbTotal.ClientID %>').innerText = "";
                    }
                } else {
                    document.getElementById('<%= lbTotal.ClientID %>').innerText = "";
                }
            }

    由于是在两个文本框中记录时间的,又不想在后台判断,所以我用的是onblur事件,总觉得不是太合适,但又没找到更好办法。

    在从stime转成st的时候,发现月份居然会加上一个月,比如我选择的是4月40号,转成st的时候就是变成5月30号,导致在计算跨月的时候的天数出错,所以只能在转换月份的时候减1,很奇怪的问题,希望知道的朋友告知一下。

  • 相关阅读:
    python基础(str,list,tuple)
    MySQL数据类型概念
    Ubuntu安装sublime
    Ubuntu安装pycharm
    ubuntu安装mysql
    微信小程序注册开发流程
    新开篇
    被玩坏了的题——马的遍历
    一道数学恶心题——小凯的疑惑
    搜索基础题:八皇后
  • 原文地址:https://www.cnblogs.com/liuxiuming/p/3082812.html
Copyright © 2011-2022 走看看