pyshon实现一天前,一月前,一年前的日期计算

直接上代码

from operator import mod
from datetime import datetime
from datetime import timedelta
import sys
import pandas as pd

timestr=sys.argv[1]
#timestr='2016-02-01'
Y=int(timestr[0:4])
M=int(timestr[6:7])
D=int(timestr[8:])
cday = datetime.strptime(timestr, '%Y-%m-%d')
oneday=cday-timedelta(days=1)

print(cday.strftime('%Y-%m-%d'))
############first#########################
oneday1=cday-pd.tseries.offsets.DateOffset(days=1)
monday1=cday-pd.tseries.offsets.DateOffset(months=1)
yday1=cday-pd.tseries.offsets.DateOffset(months=12)
print(oneday1.strftime('%Y-%m-%d'))
print(monday1.strftime('%Y-%m-%d'))
print(yday1.strftime('%Y-%m-%d'))
############second########################
if (mod(Y,4)==0)or(mod(Y,400)==0):
	if M==2 and D==29:
		monday=cday-timedelta(days=31)
		yday="N"
	elif M==2:
		monday=cday-timedelta(days=31)
		yday=cday-timedelta(days=365)
	else:
		if M==3 and D>=30:
			monday="N"
		elif M==3:
			monday=cday-timedelta(days=29)
		elif M in [1,5,7,8,10,12]:
			monday=cday-timedelta(days=30)
		else:
			monday=cday-timedelta(days=31)
		yday=cday-timedelta(days=366)
else:
	if M==3 and D>=29:
		monday="N"
	elif M==3:
		monday=cday-timedelta(days=28)
	elif M in [1,5,7,8,10,12]:
		monday=cday-timedelta(days=30)
	else:
		monday=cday-timedelta(days=31)
	yday=cday-timedelta(days=365)
print(oneday.strftime('%Y-%m-%d'))
print(monday.strftime('%Y-%m-%d'))
print(yday.strftime('%Y-%m-%d'))


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值