之前在写抓取天气预报信息的程序时,要得到明天和后天的日期,我思来想去用的方法是很老套的时间相加法来算的。

 

this_date = str(time.strftime("%Y/%m/%d %a"))
now = int(time.time())
sec = 24*60*60
day_today = "今天(%s号)" % str(time.strftime("%d", time.localtime(now+0*sec)))
day_tommo = "明天(%s号)" % str(time.strftime("%d", time.localtime(now+1*sec)))
day_aftom = "后天(%s号)" % str(time.strftime("%d", time.localtime(now+2*sec)))

 

 

 

现在想想真是可笑,我怎么忘记还有timedelta函数呢。。。

正解:

 

>>> from datetime import datetime,timedelta

>>> now = datetime.now()

>>> now

datetime.datetime(2010, 3, 5, 11, 28, 9, 453000)

>>> tomorrow = now + timedelta(hours=24)

>>> tomorrow

datetime.datetime(2010, 3, 6, 11, 28, 9, 453000)

>>> 

 

-END-

相关文章:

  • 2021-09-27
  • 2021-04-13
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2021-12-01
猜你喜欢
  • 2022-12-23
  • 2021-08-17
  • 2021-12-05
  • 2021-10-24
  • 2021-05-18
  • 2022-01-05
  • 2022-12-23
相关资源
相似解决方案