Skip to content
  • Categories
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Brite
  • 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 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D 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

      D 1 Reply Last reply
      0
      • 萧 萧云飞

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

        D Offline
        D Offline
        digitalsatori
        管理员
        wrote on last edited by
        #3

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

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

        萧 1 Reply Last reply
        0
        • D digitalsatori

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

          萧 Offline
          萧 Offline
          萧云飞
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

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