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;
      }
      }
  • 相关阅读:
    redhat Nginx 安装
    黑客经验谈:跳板攻击入侵技术实例解析
    Linux各版本的本地root密码破解方法
    python学习笔记
    Git 提交的正确姿势:Commit message 编写指南
    数论 + 扩展欧几里得
    数论
    flutter 延时函数delay Loading页面
    flutter 切换tab后保留tab状态
    iOS 应用"无法安装应用程序 因为证书无效"的解决方案
  • 原文地址:https://www.cnblogs.com/jiazhihao/p/14764513.html
Copyright © 2011-2022 走看看