0

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

  • if time is between 8:00:00 and 22:00:00 see:

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

and now is the tricky part

  • if the time is between 22:00:00 and 23:59:59 to see:

    "[[Night shift (current day number) - (next day number) (month name)]]" 
    
  • and if the time is between 00:00:00 and 8:00:00 to see:

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

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!

1 Answers1

2

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))
Byte Commander
  • 6,506
  • 6
  • 44
  • 71
  • @AntonioValentinPlătăreanu Instead of writing "Thank you" comments, please accept this answer by clicking the grey check button on its left to mark your question as solved. – Byte Commander May 04 '16 at 09:55
  • it is not working it is past 24 and it returns both [[Night shift 5 - 6 5]] and [[Day shift 6 5]] an can u please make it that it returns month in letters? i tried in the last 5 hours to figure that out with no success. ty. – Antonio Valentin Plătăreanu May 05 '16 at 21:09
  • I fixed the error you that two entries show up in the morning. To learn how to print the month as word, please read this question: http://stackoverflow.com/q/6557553/4464570 – Byte Commander May 06 '16 at 18:15