zoukankan      html  css  js  c++  java
  • js两个日期的时间差

    /**
    *

    • @param {any} oneTime 开始时间 yyyy-mm-dd hh24:min:sec
    • @param {any} twoTime 结束时间 yyyy-mm-dd hh24:min:sec
    • @param {any} stype 返回类型:
      1.SEC:相差秒数,
      2.MIN:相差分钟数,
      3.HH:相差小时数,
      4.HH:MIN:SEC:相差 “时:分:秒” 数,
      5.DD:相差天数,
      6.MM:相差月数,
      7.YY:相差年数。
      */
      function dateTimeDifference(oneTime, twoTime, stype) {
      if (oneTime == null || oneTime == "") return 0;
      if (twoTime == null || twoTime == "") return 0;
      var myoneTime = new Date(oneTime.replace(/-/g, "/"));
      var mytwoTime = new Date(twoTime.replace(/-/g, "/"));
      if (myoneTime == null || myoneTime == "") return 0;
      if (mytwoTime == null || mytwoTime == "") return 0;
      if (myoneTime > mytwoTime) return 0;
      if (stype == "SEC") {
      return (mytwoTime - myoneTime) / 1000 + "sec";
      } else if (stype == "MIN") {
      return ((mytwoTime - myoneTime) / 1000) / 60 + "min";
      } else if (stype == "HH") {
      return (((mytwoTime - myoneTime) / 1000) / 60) / 60 + "h";
      } else if (stype == "DD") {
      return ((((mytwoTime - myoneTime) / 1000) / 60) / 60) / 24 + "D";
      } else if (stype == "MM") {
      return (((((mytwoTime - myoneTime) / 1000) / 60) / 60) / 24) / 31 + "M";
      } else if (stype == "YY") {
      return ((((((mytwoTime - myoneTime) / 1000) / 60) / 60) / 24) / 31) / 365 + "Y";
      } else if (stype == "HH:MIN:SEC") {
      var seconds = (mytwoTime - myoneTime) / 1000;
      if (seconds <= 60) {
      return seconds + "sec";
      } else if (60 < seconds <= 3600) {
      return Math.floor(seconds / 60) + ":" + (seconds % 60) + "min";
      } else if (3600 < seconds <= 216000) {
      return Math.floor(seconds / 3600) + ":" + Math.floor((seconds % 3600) / 60) + ":" + ((seconds % 3600) % 60) + "h";
      }
      } else {
      return mytwoTime - myoneTime;
      }
      }
  • 相关阅读:
    Luogu P4246 [SHOI2008]堵塞的交通(线段树+模拟)
    Luogu P2619 [国家集训队2]Tree I(WQS二分+最小生成树)
    Luogu P2042 [NOI2005]维护数列(平衡树)
    Luogu P1052 过河(dp)
    Luogu P1041 传染病控制(搜索)
    Luogu P2717 寒假作业(平衡树)
    Luogu P2822 组合数问题(前缀和)
    Luogu P2827 蚯蚓(模拟)
    随机图片测试
    Luogu P2458 [SDOI2006]保安站岗(树形dp)
  • 原文地址:https://www.cnblogs.com/jiazhihao/p/14764513.html
Copyright © 2011-2022 走看看