简体   繁体   English

如何从过去的自定义日期迭代到今天?

[英]How to iterate from a custom day in the past to today?

I need to iterate through all the days from a custom day to now. 我需要遍历从自定义日期到现在的所有日子。 I need all the correct day not just the count of the days. 我需要所有正确的日子,而不仅仅是天数。

For example, I enter 10 for month and 2018 for year. 例如,我输入10表示月份,输入2018表示年份。 I need to get: 我需要得到:

2018-10-01
2018-10-02
...
2018-10-31
2018-11-01
2018-11-02
...
2019-05-21

How to increment a datetime by one day? 如何将日期时间增加一天?

https://docs.python.org/3.7/library/datetime.html https://docs.python.org/3.7/library/datetime.html

date = datetime.datetime(2007,12,5)
while date != datetime.date.today(): 
    date += datetime.timedelta(days=1)
    print(date) 

The sample uses for loop . 该示例使用for loop But, I try to use recursion. 但是,我尝试使用递归。

from datetime import timedelta, date

def getdate(date):
    print (date)
    if date == date.today(): return
    getdate(date+timedelta(1))

start_date = date(2018, 1, 1)    
getdate(start_date)

If you want to get a dataframe from pandas , the date_range function does all the work for you (doc) . 如果要从pandas获取数据帧 ,则date_range函数可以为您完成所有工作(doc) The pd.datetime.now() method gives you the current date (doc) pd.datetime.now()方法为您提供当前日期(doc)

Here one example: 这里举一个例子:

def get_df_days_to_now(year, month, day = 1):
    date_start = "%4d/%d/%d" % (year, month, day)
    date_end = pd.datetime.now().strftime("%d/%m/%Y")
    return pd.date_range(start=date_start, end=date_end, freq="D")

print(get_df_days_to_now(2019, 5))
# DatetimeIndex(['2019-05-01', '2019-05-02', '2019-05-03', '2019-05-04',
#                '2019-05-05', '2019-05-06', '2019-05-07', '2019-05-08',
#                '2019-05-09', '2019-05-10', '2019-05-11', '2019-05-12',
#                '2019-05-13', '2019-05-14', '2019-05-15', '2019-05-16',
#                '2019-05-17', '2019-05-18', '2019-05-19', '2019-05-20',
#                '2019-05-21', '2019-05-22'],
#                     dtype = 'datetime64[ns]', freq = 'D')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示今天和过去一天之间的日期 - Display the dates between today and a day in the past 如何根据从今天开始的过去日期在 Python 中查找下一天 - How to find next day of week based on past date starting with today in Python 如何从sqlalchemy中的当前时间计算过去1天的间隔 - How to calculate past 1 day interval from current time in sqlalchemy 从今天开始在 python 中查找给定日期的日期 - Find the date of a given day, from today in python 如何在 sparksql 中获取今天 -“1 天”的日期? - How to get today -"1 day" date in sparksql? 今年第一天到今天如何才能获得今年的所有约会? - How can I get all date this year from first day to today? 我如何从GET请求中获取日期值,以检查它是星期几(如果今天不是今天) - how can i get date value from the GET request, to check which week day it is(if get date is not today) 计算从今天起5天的日期,在接下来的5天(周末)中的每一天增加一天 - Calculate date 5 days from today, adding an extra day for each day in the next 5 days that is a weekend day Python 数据集 - 如何比较两个 excel 文件(一个来自前一天,另一个来自今天)以显示新记录和已删除记录? - Python dataset - how to compare two excel files (one from previous day and other from today) to show the new records and deleted records? Prometheus 导出器 - 读取 CSV 文件,其中包含过去一天的数据 - Prometheus exporter - reading CSV file having data from the past day
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM