Difference Between Two Times (python)

前端 未结 2 616
夕颜
夕颜 2020-12-11 14:29

Just want to know if i can set the variable a sign off time to midnight.

i want sign off to be a raw_input and midnight to be fixed value

so far i have this:

2条回答
  •  無奈伤痛
    2020-12-11 14:46

    You don't see to use signOnSun1 so not exactly sure what you want but this should be closer to what you want :

    from datetime import datetime
    #Variablesr
    FMT = '%H:%M' #Time Format use HH:MM
    rate1 = 35.34 #Base Hourly Rate
    rate2 = 35.34 #Base Hourly Rate
    rate3 = 35.34 #Base Hourly Rate
    rate4 = 35.34 #Base Hourly Rate
    rate5 = 35.34 #Base Hourly Rate
    rate6 = 50 #Base Hourly Rate
    rate7 = 70 #Base Hourly Rate
    
    Midnight = "00:00" # make midnight a string
    
    amp = 2.40 #Morning shift penalties
    pmp = 2.50 #Afternoon shift penalties
    ns = 4.4 #Night shift penalties
    cabAll = 8.34 #Cab Allowance
    # make sure user know format to use
    signOnSun1 = raw_input("What time did you sign on Sunday, enter in format HH:MM: ")
    signOffSun1 = raw_input("What time did you sign off Sunday, enter in format HH:MM: ")
    # use Midnight string and FMT
    diff = (datetime.strptime(signOffSun1, FMT) - datetime.strptime(Midnight,FMT))
    print diff
    

    Really you should make sure the user enters correct data using a try/except block and you will get caught if you comparing times before and after midnight

    If all times are up to midnight you can hardcode the dates setting midnight to one day after:

    FMT = '%H:%M-%Y-%m-%d' #Time Format
    rate1 = 35.34 #Base Hourly Rate
    rate2 = 35.34 #Base Hourly Rate
    rate3 = 35.34 #Base Hourly Rate
    rate4 = 35.34 #Base Hourly Rate
    rate5 = 35.34 #Base Hourly Rate
    rate6 = 50 #Base Hourly Rate
    rate7 = 70 #Base Hourly Rate
    
    Midnight = "00:00-1900-01-02"
    
    amp = 2.40 #Morning shift penalties
    pmp = 2.50 #Afternoon shift penalties
    ns = 4.4 #Night shift penalties
    cabAll = 8.34 #Cab Allowance
    
    signOnSun1 = raw_input("What time did you sign on Sunday, enter in format HH:MM: ")
    signOffSun1 = raw_input("What time did you sign off Sunday, enter in format HH:MM: ")
    diff = datetime.strptime(Midnight, FMT) - datetime.strptime("{}-1900-01-01".format(signOnSun1), FMT)
    print diff
    

提交回复
热议问题