How to subtract a day from a date?
我有一个python
可以使用TimeDelta对象:
1 2 3 | from datetime import datetime, timedelta d = datetime.today() - timedelta(days=days_to_subtract) |
减去EDOCX1[0]
如果您的python datetime对象了解时区,则应小心避免DST转换周围的错误(或由于其他原因而更改UTC偏移量):
1 2 3 4 5 6 7 8 9 | from datetime import datetime, timedelta from tzlocal import get_localzone # pip install tzlocal DAY = timedelta(1) local_tz = get_localzone() # get local timezone now = datetime.now(local_tz) # get timezone-aware datetime object day_ago = local_tz.normalize(now - DAY) # exactly 24 hours ago, time may differ naive = now.replace(tzinfo=None) - DAY # same time yesterday = local_tz.localize(naive, is_dst=None) # but elapsed hours may differ |
号
一般来说,如果本地时区的UTC偏移量在最后一天发生了变化,则
例如,夏令时/夏令时将于2014年11月2日下午2:00:00在美国/洛杉矶时区结束,因此,如果:
1 2 3 4 5 | import pytz # pip install pytz local_tz = pytz.timezone('America/Los_Angeles') now = local_tz.localize(datetime(2014, 11, 2, 10), is_dst=None) # 2014-11-02 10:00:00 PST-0800 |
那么,
day_ago 正好是24小时前(相对于now 时),但上午11点,而不是上午10点,因为now 时。yesterday 是昨天上午10点,但它是25小时前(相对于now 而言),而不是24小时。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> import pendulum # $ pip install pendulum >>> now = pendulum.create(2014, 11, 2, 10, tz='America/Los_Angeles') >>> day_ago = now.subtract(hours=24) # exactly 24 hours ago >>> yesterday = now.subtract(days=1) # yesterday at 10 am but it is 25 hours ago >>> (now - day_ago).in_hours() 24 >>> (now - yesterday).in_hours() 25 >>> now <Pendulum [2014-11-02T10:00:00-08:00]> >>> day_ago <Pendulum [2014-11-01T11:00:00-07:00]> >>> yesterday <Pendulum [2014-11-01T10:00:00-07:00]> |
。
只是为了阐述一种替代方法和一个有用的用例:
- 从当前日期时间中减去1天:
1
2 from datetime import datetime, timedelta
print datetime.now() + timedelta(days=-1) # Here, I am adding a negative timedelta
号
- 在这种情况下很有用,如果您想添加5天,并从当前日期时间中减去5小时。也就是说,从现在起5天内,5小时内的日期时间是多少?
1
2 from datetime import datetime, timedelta
print datetime.now() + timedelta(days=5, hours=-5)
号
它同样可以与其他参数一起使用,例如秒、周等。
存在genial arrow模块
1 2 3 4 5 | import arrow utc = arrow.utcnow() utc_yesterday = utc.shift(days=-1) print(utc, ' ', utc_yesterday) |
。
输出:
1 2 | 2017-04-06T11:17:34.431397+00:00 2017-04-05T11:17:34.431397+00:00 |
。
还有一个很好的函数,我喜欢在我想计算的时候使用,比如上个月的第一天/最后一天,或者其他相对的时间增量等等。
dateutil函数中的relativedelta函数(对datetime lib的强大扩展)
1 2 3 4 5 6 7 8 9 | import datetime as dt from dateutil.relativedelta import relativedelta #get first and last day of this and last month) today = dt.date.today() first_day_this_month = dt.date(day=1, month=today.month, year=today.year) last_day_last_month = first_day_this_month - relativedelta(days=1) print (first_day_this_month, last_day_last_month) >2015-03-01 2015-02-28 |
我建议使用
使用
1 2 3 4 5 | import pendulum dt = pendulum.datetime(2012, 1, 31) dt.to_datetime_string() # '2019-03-26 23:39:29' |
1 2 3 4 5 6 | dt.subtract(days=1) # '2019-03-25 23:39:29' # You can subtract different/multiple units of time. dt.subtract(years=3, months=2, days=6, hours=12, minutes=31, seconds=43) # '2012-01-28 00:00:00' |
。
Note Using
add_timedelta() andsub_timedelta() , you can add or subtracttimedelta objects, which is useful if you already have
these.
1
2
3
4
5
6 # You can also add or remove a timedelta
dt.add_timedelta(timedelta(hours=3, minutes=4, seconds=5))
# '2012-01-28 03:04:05'
dt.sub_timedelta(timedelta(hours=3, minutes=4, seconds=5))
# '2012-01-28 00:00:00'
号
同样,可以使用
1 2 3 4 5 6 7 8 9 | dt.add(days=1) # '2019-03-27 23:39:29' dt.add(years=5) '2017-01-31 00:00:00' # You can add different/multiple units of time. dt.add(years=3, months=2, days=6, hours=12, minutes=31, seconds=43) # '2015-04-03 12:31:43' |