output of day/night shift times depending on current clock time

I needed to see accordingly to what time is now the following:

and now is the tricky part

Made it to work in Excel I'm not experienced with Python.

=IF(AND(MOD(Sheet4!A1,1)>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)TIME(0,0,59),MOD(Sheet4!A1,1)

I am unable to show you all the excel formula :/ no idea why

in sheet4 a1 put =NOW()

Many Thanks!

Upvotes: 0

Views: 1085

Answers (1)

Byte Commander
Byte Commander

Reputation: 6736

Use the datetime module:

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))

Upvotes: 2

Related Questions