⑴ python怎麼獲取昨天的日期,比如今天25號,我想獲得的結果是4-24
importdatetime
now=datetime.datetime.now()
yesterday=now-datetime.timedelta(days=1)
print(yesterday.strftime("%Y-%m-%d%H:%M:%S"))
print(yesterday.strftime("%Y-%m-%d"))
print(yesterday.strftime("%m-%d"))
⑵ python如何輸出日期
a,b,c=map(int,input().split('/'))
print('%d年%d月%d日'%(a,b,c))
⑶ python生成時間列表
python生成桐碼時間列表
yearmonthday = pd.date_range("2020-07-01"森輪唯,"2020-12-31",freq="D").strftime("%Y%m%d").to_list()
month = pd.date_range("2020-07-01","2020-12-31", freq="M" ). strftime("%Y%m") .to_list()
day = pd.date_range("2020-07-01","2020-12-31",freq="D").strftime("此培%d").to_list()
⑷ python顯示農歷日期
需安裝sxtwl包,命令如下
pip install sxtwl
農歷(Lunar calendar)<->陽歷(solar calendar)轉換
代碼如下(基於python3):
import sxtwl
#日歷中文索引
ymc = [u"十一", u"十二", u"正", u"二", u"三", u"四", u"五", u"六", u"七", u"八", u"九", u"十" ]
rmc = [u"初一", u"初二", u"初三", u"初四", u"初五", u"初六", u"初七", u"初八", u"初九", u"初十", \
u"十一", u"十二", u"十三", u"十四", u"十五", u"十六", u"十七", u"十八", u"十九", \
u"二十", u"廿一", u"廿二", u"廿三", u"廿四", u"廿五", u"廿六", u"廿七", u"廿八", u"廿九", u"三十", u"卅一"]
# 日歷庫實例化
lunar = sxtwl.Lunar()
# 1.陰歷轉陽歷
solar_day = lunar.getDayByLunar(2019,7,27)
print("公歷(陽歷):{0}年{1}月{2}日".format(solar_day.y, solar_day.m, solar_day.d))
# 2.陽歷轉陰歷
from datetime import date
today = str(date.today()) # 如 2019-08-08
today_list = today.split('-') # ['2019', '08', '08']
lunar_day = lunar.getDayBySolar((int)(today_list[0]),(int)(today_list[1]),(int)(today_list[2])) # 輸入年月日
# 判斷是否為潤年
if(lunar_day.Lleap):
print("陰歷(農歷):{0}月{1}".format(ymc[lunar_day.Lmc], rmc[lunar_day.Ldi]))
else:
print("陰歷(農歷):{0}月{1}".format(ymc[lunar_day.Lmc], rmc[lunar_day.Ldi]))
結果為
'''
公歷(陽歷):2019年8月27日
陰歷(農歷):七月初八
'''
⑸ python中,怎麼把字元串轉換為日期格式
1、新建python文件,testtime.py;