Skip to content
  • Categories
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Flatly)
  • No Skin
Collapse

Odoo 中文社区

  1. Home
  2. Categories
  3. Python 开发
  4. 如何用python快速的算出两个时间段中重叠的天数

如何用python快速的算出两个时间段中重叠的天数

Scheduled Pinned Locked Moved Python 开发
4 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • digitalsatoriD Offline
    digitalsatoriD Offline
    digitalsatori 管理员
    wrote on last edited by
    #1

    比如一个时间段是:2018年1月15日到2018年5月10日
    另一个时间段是:2018年3月20日到2018年9月15日
    计算重叠天数

    >>> from datetime import datetime
    >>> from collections import namedtuple
    >>> Range = namedtuple('Range', ['start', 'end'])
    
    
    >>> r1 = Range(start=datetime(2018, 1, 15), end=datetime(2018, 5, 10))
    >>> r2 = Range(start=datetime(2018, 3, 20), end=datetime(2018, 9, 15))
    >>> latest_start = max(r1.start, r2.start)
    >>> earliest_end = min(r1.end, r2.end)
    >>> delta = (earliest_end - latest_start).days + 1
    >>> overlap = max(0, delta)
    >>> overlap
    52
    

    【上海先安科技】(tony AT openerp.cn)

    1 Reply Last reply
    2
    • 萧 Offline
      萧 Offline
      萧云飞
      wrote on last edited by
      #2

      哈哈哈,大佬这个题是集思广益,我来献下丑:
      0_1517449691859_360桌面截图20180201094659.jpg

      digitalsatoriD 1 Reply Last reply
      0
      • digitalsatoriD Offline
        digitalsatoriD Offline
        digitalsatori 管理员
        replied to 萧云飞 on last edited by
        #3

        @萧云飞 感谢提供你的方案,棒!
        解决的思路是类似的,不过兄弟没有考虑当两个时间段没有交集的情况:face_with_stuck-out_tongue_winking_eye:

        【上海先安科技】(tony AT openerp.cn)

        萧 1 Reply Last reply
        0
        • 萧 Offline
          萧 Offline
          萧云飞
          replied to digitalsatori on last edited by
          #4

          @digitalsatori 就是,哈哈,被您一说,才发现。这个就只能提前判断是否有交汇了:grinning_face_with_smiling_eyes:

          1 Reply Last reply
          0

          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Tags
          • Popular
          • Users
          • Groups