How do I round the time to last quarter hour for the current time. I am able to find the last quarter hour minute, however, not able to fit this into the correct time format.
import datetime
import time
time = datetime.datetime.now()
last_quarter_minute = 15*(time.minute//15)
current_time = time.strftime("%Y/%m/%d %H:%M")
print last_quarter_minute
print current_time
Ideally, I want to be able to compare the time I get from a log, to the last quarter time.
You need to replace minute part of the time, using datetime.datetime.replace (returns a new datetime object with the specifieid field replaced):
>>> time.strftime("%Y/%m/%d %H:%M")
'2016/05/13 12:57'
>>> time.replace(minute=last_quarter_minute).strftime("%Y/%m/%d %H:%M")
'2016/05/13 12:45'
To be more precise, you need to also set second, microsecond also.
Related
My code is the following:
date = datetime.datetime.now()- datetime.datetime.now()
print date
h, m , s = str(date).split(':')
When I print h the result is:
-1 day, 23
How do I get only the hour (the 23) from the substract using datetime?
Thanks.
If you subtract the current date from a past date, you would get a negative timedelta value.
You can get the seconds with td.seconds and corresponding hour value via just dividing by 3600.
from datetime import datetime
import time
date1 = datetime.now()
time.sleep(3)
date2 = datetime.now()
# timedelta object
td = date2 - date1
print(td.days, td.seconds // 3600, td.seconds)
# 0 0 3
You're not too far off but you should just ask your question as opposed to a question with a "real scenario" later as those are often two very different questions. That way you get an answer to your actual question.
All that said, rather than going through a lot of hoop-jumping with splitting the datetime object, assigning it to a variable which you then later use look for what you need in, it's better to just know what DateTime can do since that can be such a common part of your coding. You would also do well to look at timedelta (which is part of datetime) and if you use pandas, timestamp.
from datetime import datetime
date = datetime.now()
print(date)
print(date.hour)
I can get you the hour of datetime.datetime.now()
You could try indexing a list of a string of datetime.datetime.now():
print(list(str(datetime.datetime.now()))[11] + list(str(datetime.datetime.now()))[12])
Output (in my case when tested):
09
Hope I am of help!
I want to make a code in python to find out the duration an employee had worked by taking two inputs.
input1 will be the time when the employee arrived at work, in hh:mm format.
input2 will be the time when the employee left work, in hh:mm format.
I want to calculate the duration an employee worked in hh:mm format when I subtract input1 from input2.
Please help in making this code.
There is no timedelta attribute for time objects.
You can use a datetime object first, then do:
difference = datetime1 - datetime2
The, finally, do:
print(difference.strftime('%H:%M')
Alternatively, if you are only allowed to use time input/object, you can do:
import datetime
# Time in example
a = datetime.time(9, 30)
# Time out example
b = datetime.time(17, 45)
hours = abs(int(b.strftime('%H')) - int(a.strftime('%H')))
minutes = abs(int(b.strftime('%M')) - int(a.strftime('%M')))
time_difference = datetime.time(hours, minutes)
print(time_difference)
How to add hours to a timestamp in python?
I have a timestamp in UTC, and I want to add hours to it. If I convert it to DateTime, it changes to local time. So, I want to add hours directly to the timestamp without converting to datetime.
import time
from datetime import datetime, timedelta
input = datetime(year=datetime.now().year, month=datetime.now().month, day=datetime.now().day)
today = datetime.now()
days = (today - input).days
d = datetime(year=today.year, month=today.month, day=today.day) - timedelta(seconds=time.timezone) - timedelta(days=days)
utc_midnight_timestamp = int(time.mktime(d.utctimetuple()))
Now I want to keep adding 1 hour to the utc_midnight_timestamp many times
for x in range(1,24):
#Need to keep incrementing utc_midnight_timestamp by x hours
#use timestamp later
I am a beginner in Python. I am wondering about does Python has Real Time Clock which is giving us exactly year, month, day, hour, min, and second?
Thank you for your help!
For Date Time operations in python you need to import datetime library.There different functions related to date formatting, getting current date and time etc.. Refer this documentation
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))
Hope the above code will do your job ...
datetime.now()
Thats it.It gives the current date and time upto microseconds.
Use as you want.
Setting an alarm after 2 days.
alarm_time = date.today() + timedelta(days = 2) #set alarm time 2day after today
while(date.today()!=alarm_time): #run loop till datetoday is not alarm_time
time.sleep(60*60) #wait for an hour then recheck time
#do here anything you want to do when alarm time has reached#
you can try this:
temp.year, temp.month, temp.day,temp.hour,temp.minute,temp.second would give your desire values separately
from datetime import datetime
temp = datetime.now()
timee = "{:04d}{:02d}{:02d} {:02d}:{:02d}:{:02d}".format(temp.year, temp.month, temp.day,temp.hour,temp.minute,temp.second)
Obviously I can get the date and time from datetime.datetime.now(), but I don't actually care about the seconds or especially microseconds.
Is there somewhere I can easily get Date+Hour+Minute?
You can clear down the second and microsecond component of a datetime value like so:
dt = datetime.datetime.now()
#Now get rid of seconds and microseconds component:
dt = dt.replace(second=0, microsecond=0)
This would allow you to compare datetimes to minute granularity.
If you just want to print the date without a second/microsecond component, then use the appropriate format string:
dt = datetime.datetime.now()
print dt.strftime("%Y/%m/%d %H:%M")
>>> '2012/12/12 12:12'