📜  Python - 将日期范围划分为 N 个相等的持续时间

📅  最后修改于: 2022-05-13 01:54:39.647000             🧑  作者: Mango

Python - 将日期范围划分为 N 个相等的持续时间

给定两个日期,任务是将其划分为相等的持续时间,并获得每个划分点的确切时间。

方法#1:使用循环

在此,我们使用整个持续时间除以 N 来计算每个段的持续时间。在此之后,每个日期是使用循环中的段持续时间乘法构建的。

Python3
# Python3 code to demonstrate working of
# Convert date range to N equal durations
# Using loop
import datetime
  
# initializing dates
test_date1 = datetime.datetime(1997, 1, 4)
test_date2 = datetime.datetime(1997, 1, 30)
               
# printing original dates
print("The original date 1 is : " + str(test_date1))
print("The original date 2 is : " + str(test_date2))
  
# initializing N
N = 7
  
temp = []
  
# getting diff.
diff = ( test_date2 - test_date1) // N
for idx in range(0, N):
      
    # computing new dates
    temp.append((test_date1 + idx * diff))
  
# using strftime to convert to userfriendly 
# format
res = []
for sub in temp:
  res.append(sub.strftime("%Y/%m/%d %H:%M:%S"))
  
# printing result
print("N equal duration dates : " + str(res))


Python3
# Python3 code to demonstrate working of
# Convert date range to N equal durations
# Using generator function
import datetime
  
def group_util(test_date1, test_date2, N):
      
    diff = (test_date2  - test_date1 ) / N
    for idx in range(N):
          
        # using generator function to solve problem
        # returns intermediate result
        yield (test_date1 + diff * idx)
    yield test_date2
      
# initializing dates
test_date1 = datetime.datetime(1997, 1, 4)
test_date2 = datetime.datetime(1997, 1, 30)
               
# printing original dates
print("The original date 1 is : " + str(test_date1))
print("The original date 2 is : " + str(test_date2))
  
# initializing N
N = 7
  
# calling generator expression
temp = list(group_util(test_date1, test_date2, N))
  
# using strftime to convert to userfriendly format
res = []
for sub in temp:
  res.append(sub.strftime("%Y/%m/%d %H:%M:%S"))
  
# printing result
print("N equal duration dates : " + str(res))


输出:

方法#2:使用生成器函数

在这种情况下,我们使用生成器函数而不是循环方法执行产生中间结果的任务。唯一的区别是返回中间结果。

蟒蛇3

# Python3 code to demonstrate working of
# Convert date range to N equal durations
# Using generator function
import datetime
  
def group_util(test_date1, test_date2, N):
      
    diff = (test_date2  - test_date1 ) / N
    for idx in range(N):
          
        # using generator function to solve problem
        # returns intermediate result
        yield (test_date1 + diff * idx)
    yield test_date2
      
# initializing dates
test_date1 = datetime.datetime(1997, 1, 4)
test_date2 = datetime.datetime(1997, 1, 30)
               
# printing original dates
print("The original date 1 is : " + str(test_date1))
print("The original date 2 is : " + str(test_date2))
  
# initializing N
N = 7
  
# calling generator expression
temp = list(group_util(test_date1, test_date2, N))
  
# using strftime to convert to userfriendly format
res = []
for sub in temp:
  res.append(sub.strftime("%Y/%m/%d %H:%M:%S"))
  
# printing result
print("N equal duration dates : " + str(res))

输出: