明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 699|回复: 1

[函数] 计算时间差,返回两个时间点之间的分钟数

  [复制链接]
发表于 2019-9-20 13:24 | 显示全部楼层 |阅读模式
大部分是借鉴了这个帖子:http://bbs.mjtd.com/forum.php?mod=viewthread&tid=95113&highlight=%CA%B1%BC%E4%B2%EE
改成了返回分钟数
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;日期差计算,借鉴http://bbs.mjtd.com/forum.php?mod=viewthread&tid=95113&highlight=%CA%B1%BC%E4%B2%EE

(defun dt-dates        (oneyear)
  (if (numberp oneyear)
    (if        (= 0 (rem oneyear 4))
      (if (= 0 (rem oneyear 100))
        (if (= 0 (rem oneyear 400))
          366
          365
        )
        366
      )
      365
    )
  )
)
;;;返回指定年份每个月和对应的天数的关联表
(defun dt-everyMonth-list (spacYear)
  (list
    (cons 1 31)
    (cons 2
          (if (= 0 (rem spacYear 4))
            (if        (= 0 (rem spacYear 100))
              (if (= 0 (rem spacYear 400))
                29
                28
              )
              29
            )
            28
          )
    )
    (cons 3 31)
    (cons 4 30)
    (cons 5 31)
    (cons 6 30)
    (cons 7 31)
    (cons 8 31)
    (cons 9 30)
    (cons 10 31)
    (cons 11 30)
    (cons 12 31)
  )
)
;;;返回指定日期是当年的第几天
;;;spyear  指定年份
;;;spmonth 指定月份
;;;spdate  指定日期
(defun dt-nthdate (spyear spmonth spdate / ddList somemon sumdates)
  (if (and (numberp spyear) (numberp spmonth) (numberp spdate))
    (progn
      (setq ddList   (dt-everyMonth-list spyear)
            somemon  1
            sumdates 0
      )
      (while (< somemon spmonth)
        (setq sumdates (+ (cdr (assoc somemon ddList)) sumdates)
              somemon  (1+ somemon)
        )
      )
      (+ sumdates spdate)
    )
  )
)
;;;两个日期之间的天数例如(DT-DIST '(2019 3 2) '(2018 5 2))
(defun dt-dist (mList nList / maxyear minyear maxList minList someyear
                sumdate)
  (if (and (numberp (caddr mList)) (numberp (caddr nList)))
    (if        (/= (car mList) (car nList))
      (progn
        (setq maxyear (max (car mList) (car nList))
              minyear (min (car mList) (car nList))
              maxList (assoc maxyear (list mList nList))
              minList (assoc minyear (list mList nList))
        )
        (setq someyear (+ minyear 1)
              sumdate  0
        )
        (while (< someyear maxyear)
          (setq        sumdate         (+ (dt-dates someyear) sumdate)
                someyear (1+ someyear)
          )
        )
        (+ (- (dt-dates minyear)
              (dt-nthdate minyear (cadr minList) (caddr minList))
           )
           sumdate
           (dt-nthdate maxyear (cadr maxList) (caddr maxList))
        )
      )
      (abs (- (dt-nthdate (car mList) (cadr mList) (caddr mList))
              (dt-nthdate (car nList) (cadr nList) (caddr nList))
           )
      )
    )
  )
)


(defun time_format(t1)
  (list
    (list "y"(atoi(substr t1 1 4)))
    (list "mo"(atoi(substr t1 5 2)))
    (list "d"(atoi (substr t1 7 2)) )
    (list "h"(atoi(substr t1 10 2)) )
    (list "mi"(atoi (substr t1 12 2)))))
(defun time_value_get(ftime lab)
  (nth 1 (assoc lab ftime)))

(defun time-time(t1 t2);;;;时间相减,返回分钟“20190919-1739”,t1较大时间,t2较小时间
  (setq t1lst(time_format t1)
        t2lst(time_format t2))
  (setq daydist(dt-dist (list (time_value_get t1lst "y")(time_value_get t1lst "mo")(time_value_get t1lst "d"))
                        (list (time_value_get t2lst "y")(time_value_get t2lst "mo")(time_value_get t2lst "d"))));;;;天数差
  ( setq mdist (+(* daydist 24 60)
                 (* 60(- (time_value_get t1lst "h")(time_value_get t2lst "h")))
                 (* 1(- (time_value_get t1lst "mi")(time_value_get t2lst "mi")))))
        mdist               
  )


示例:(time-time  "20190919-1739" "20180819-1739" )
返回:570240

点评

好厉害,我只弄个小小的: (defun S时间1()(setq time1 (atof(menucmd"M=$(edtime,$(getvar,date),YYMODDhhmmss)") ) ) ) (defun S时间2()(setq time2 (atof(menucmd"M=$(edtime,$(getvar,date),YYMODDhhm   发表于 2019-9-20 13:55
"觉得好,就打赏"
还没有人打赏,支持一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-23 19:16 , Processed in 0.307316 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表