首页 > 编程语言 > 详细

Python 获得最近一个月的每天的日期

时间:2019-04-17 12:48:07      阅读:298      评论:0      收藏:0      [点我收藏+]

直接上代码:

#coding:utf-8
# from common.contest import *


import datetime
import time
begin_date = (datetime.datetime.now() - datetime.timedelta(days =30)).strftime("%Y-%m-%d")
date_list = []
begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
end_date = datetime.datetime.strptime(time.strftime(%Y-%m-%d,time.localtime(time.time())), "%Y-%m-%d")
while begin_date <= end_date:
    date_str = begin_date.strftime("%Y-%m-%d")
    date_list.append(date_str)
    begin_date += datetime.timedelta(days=1)


print date_list

 

输出结果是:

C:\Python27\python.exe E:/squid_frame/weibo_pc_spider_local/test.py
[2019-03-18, 2019-03-19, 2019-03-20, 2019-03-21, 2019-03-22, 2019-03-23, 2019-03-24, 2019-03-25, 2019-03-26, 2019-03-27,
2019-03-28, 2019-03-29, 2019-03-30, 2019-03-31, 2019-04-01, 2019-04-02, 2019-04-03, 2019-04-04, 2019-04-05, 2019-04-06,
2019-04-07, 2019-04-08, 2019-04-09, 2019-04-10, 2019-04-11, 2019-04-12, 2019-04-13, 2019-04-14, 2019-04-15, 2019-04-16,
2019-04-17] Process finished with exit code 0

 

Python 获得最近一个月的每天的日期

原文:https://www.cnblogs.com/xuchunlin/p/10722623.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!