日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python】时间处理:日期减少n年(考虑闰年)

發(fā)布時間:2024/2/28 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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年(考虑闰年)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。