python中datetime模块的使用
2021/4/13 20:27:03
本文主要是介绍python中datetime模块的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python中datetime模块的使用方法
导入包 import datetime 获取当天日期 now_time=datetime.datetime.now() print((now_time+datetime.timedelta(days=+1)).strftime("%Y-%m-%d %H:%M:%S")) #获取后一天+1 前一天-1 print ((now_time+datetime.timedelta(hours=-1)).strftime("%Y-%m-%d %H:%M:%S")) #获取前一小时 print(now_time.strftime("%Y-%m-%d %H:%M:%S"),(now_time+datetime.timedelta(minutes=+1)).strftime("%Y-%m-%d %H:%M:%S")) #前一分钟后一分钟 print(now_time.strftime("%Y-%m-%d %H:%M:%S"),(now_time+datetime.timedelta(minutes=+1/60)).strftime("%Y-%m-%d %H:%M:%S"))#前一秒后一秒
# 导入包 import calendar import datetime #获取一个月或是N个月,一年中的天数 years = [2019,] months = range(1, 13) for year in years: for month in months: days = calendar.monthrange(int(year), int(month))[1] for day in range(1,(days)+1): format_datetime = datetime.datetime(year, month, day).strftime('%Y-%m-%d') print(format_datetime)
import calendar import datetime years = [2019,] months = range(1, 13) for year in settings.years: for month in settings.months: day = calendar.monthrange(int(year), int(month))[1] fromdate = datetime.date(year, month, 1) # 开始时间: 2019-01-01 todate = fromdate + datetime.timedelta(days=day + 1) # 结束时间: 2019-02-02
这篇关于python中datetime模块的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python