Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

datetime - Difference Between Two Times (python)

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:

#!/usr/bin/python
from datetime import datetime
from Tkinter import *
import math
#Variablesr
FMT = '%H%M' #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 = 0000,FMT
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: ");
signOffSun1 = raw_input("What time did you sign off Sunday: ");

diff = (datetime.strptime(signOffSun1, FMT) - datetime.strptime (Midnight, FMT))
print diff
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...