根据当前时钟时间输出日/夜班次

时间:2016-05-04 08:02:16

标签: python-2.7 datetime

我需要相应地看到现在的时间如下:

  • 如果时间在8:00:00到22:00:00之间,请参阅:

    "[[Day shift (current day number) (month name)]]"
    

现在是棘手的部分

  • 如果时间在22:00:00到23:59:59之间,请看:

    "[[Night shift (current day number) - (next day number) (month name)]]" 
    
  • 如果时间在00:00:00到8:00:00之间,请看:

    "[[Night shift (previous day number) - (current day number) (month name)]]"
    

让它在Excel中工作我对Python没有经验。

= IF(AND(MOD(Sheet4 A1,1)GT;!TIME(8,0,0),MOD(Sheet4 A1,1)TIME(8,0,0),MOD(Sheet4 A1, 1)TIME(20,0,0),MOD(Sheet4!A1,1)TIME(20,0,0),MOD(Sheet4!A1,1)TIME(0,0,59),MOD(Sheet4!A1 1)时间(0,0,59),MOD(Sheet4!A1,1)

我无法向您展示所有excel公式:/不知道为什么

sheet4中的

a1 put = NOW()

非常感谢!

1 个答案:

答案 0 :(得分:1)

使用datetime模块:

from datetime import datetime, timedelta
now = datetime.now()

if now.hour < 8:
    print("[[Night shift {yesterday.day} - {today.day} {today.month}]]"
          .format(today=now, yesterday=now-timedelta(1)))
elif now.hour >= 22:
    print("[[Night shift {today.day} - {tomorrow.day} {today.month}]]"
          .format(today=now, tomorrow=now+timedelta(1)))
else:
    print("[[Day shift {today.day} {today.month}]]".format(today=now))