【Python】时间处理:日期减少n年(考虑闰年)
生活随笔
收集整理的這篇文章主要介紹了
【Python】时间处理:日期减少n年(考虑闰年)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
終極版
# -*-coding:utf-8-*- import calendar import datetime import timefrom dateutil.relativedelta import relativedeltadef get_date_bef(date_ori, n):'''獲取距離date_ori n年前的時間'''# 考慮閏年d = datetime.datetime.strptime(date_ori, '%Y-%m-%d %H:%M:%S')date_str = (d - relativedelta(years=n)).strftime('%Y-%m-%d %H:%M:%S')date_time = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')print date_oriprint date_timeprint ''return date_timedef get_date_str():date_now = time.localtime(time.time())date_now_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))return date_now_strdate_now_str = get_date_str() date_n_year_bef = get_date_bef(date_now_str, 1) date_n_year_bef = get_date_bef('2019-02-28 21:09:52', 1) date_n_year_bef = get_date_bef('2018-02-28 21:09:52', 2) date_n_year_bef = get_date_bef('2017-02-28 21:09:52', 3) date_n_year_bef = get_date_bef('2016-02-29 21:09:52', 1) date_n_year_bef = get_date_bef('2016-02-29 21:09:52', 2) date_n_year_bef = get_date_bef('2016-02-29 21:09:52', 3) date_n_year_bef = get_date_bef('2016-02-29 21:09:52', 4)輸出
C:\Python27\python.exe C:/Users/Bug/PycharmProjects/studypython/test.py 2019-11-27 21:11:11 2018-11-27 21:11:112019-02-28 21:09:52 2018-02-28 21:09:522018-02-28 21:09:52 2016-02-28 21:09:522017-02-28 21:09:52 2014-02-28 21:09:522016-02-29 21:09:52 2015-02-28 21:09:522016-02-29 21:09:52 2014-02-28 21:09:522016-02-29 21:09:52 2013-02-28 21:09:522016-02-29 21:09:52 2012-02-29 21:09:52Process finished with exit code 0舊版本:
給定日期減少n年
# -*-coding:utf-8-*- import calendar import datetimedate_now = datetime.datetime.now()def get_date_bef(date_ori, n):from dateutil.relativedelta import relativedeltad = datetime.datetime.strptime(date_ori, '%Y-%m-%d').date()print(date_ori)print((d - relativedelta(years=n)).strftime('%Y-%m-%d'))print ''get_date_bef('2019-11-27',1) get_date_bef('2019-02-28',1) get_date_bef('2018-02-28',1) get_date_bef('2017-02-28',1) get_date_bef('2016-02-28',1) get_date_bef('2016-02-29',1) get_date_bef('2016-02-29',2) get_date_bef('2016-02-29',3) get_date_bef('2016-02-29',4) get_date_bef('2016-02-29',5)輸出結果
C:\Python27\python.exe C:/Users/Bug/PycharmProjects/studypython/test.py 2019-11-27 2018-11-272019-02-28 2018-02-282018-02-28 2017-02-282017-02-28 2016-02-282016-02-28 2015-02-282016-02-29 2015-02-282016-02-29 2014-02-282016-02-29 2013-02-282016-02-29 2012-02-292016-02-29 2011-02-28Process finished with exit code 0使用當前時間減少n年
# -*-coding:utf-8-*- import calendar import datetime import timefrom dateutil.relativedelta import relativedeltadate_now = time.localtime(time.time()) date_now_str = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))def get_date_bef(date_now_str, n):d = datetime.datetime.strptime(date_now_str, '%Y-%m-%d %H:%M:%S')print(d)print((d - relativedelta(years=n)).strftime('%Y-%m-%d %H:%M:%S'))print ''get_date_bef(date_now_str,1) get_date_bef(date_now_str,2) get_date_bef(date_now_str,3) get_date_bef(date_now_str,4) get_date_bef(date_now_str,5) get_date_bef(date_now_str,6) get_date_bef(date_now_str,7) get_date_bef(date_now_str,8)輸出結果
C:\Python27\python.exe C:/Users/Bug/PycharmProjects/studypython/test.py 2019-11-27 20:56:01 2018-11-27 20:56:012019-11-27 20:56:01 2017-11-27 20:56:012019-11-27 20:56:01 2016-11-27 20:56:012019-11-27 20:56:01 2015-11-27 20:56:012019-11-27 20:56:01 2014-11-27 20:56:012019-11-27 20:56:01 2013-11-27 20:56:012019-11-27 20:56:01 2012-11-27 20:56:012019-11-27 20:56:01 2011-11-27 20:56:01Process finished with exit code 0總結
以上是生活随笔為你收集整理的【Python】时间处理:日期减少n年(考虑闰年)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【EasyUI】DataGrid实现表格
- 下一篇: websocket python爬虫_p