I need to get the current date and time upon running the program, both as their respective objects in the datetime library.
I have been able to get a date object for the current date fine:
datetime.date.today()
but how can i get a time object for the current time? datetime.time.now() doesn't work, and datetime.time() is used to instantiate a time object by passing in your own values, but i want to just get a time object with the current time information.
>>> import datetime
>>> n = datetime.datetime.now()
>>> n
datetime.datetime(2020, 8, 23, 12, 37, 51, 595180)
>>> n.date()
datetime.date(2020, 8, 23)
>>> n.time()
datetime.time(12, 37, 51, 595180)
Related
I am moving my working Python into somebody else's revised Python code. I get errors. I understand that datetime.datetime cause problems, but I think I have tried the permutations. In testing, if I return() before the lines below, no runtime error. I am sorry about the formatting in the post. I have not figured that out properly.
Python version 2.7.12
import os, sys, re, datetime
from datetime import datetime
#NOTE: commenting out "from datetime import datetime" does not change the errors generated, so I think something is importing that from somewhere.
These are the 3 versions and the resulting error message at run time"
dt = dt - datetime.timedelta(hours=24.5) # decrement date a day or two
gives error "type object 'datetime.datetime' has no attribute 'timedelta'"
dt = dt - datetime.datetime.timedelta(hours=24.5) # ; also error:
gives error" type object 'datetime.datetime' has no attribute 'datetime'"
dt = dt - timedelta(hours=24.5) # ; also error:
gives error "global name 'timedelta' is not defined"
You say that commenting out from datetime import datetime does not change the errors, but this shouldn't be the case, it is the cause of the problem.
timedelta is in the datetime module. However, because you have done from datetime import datetime, the name datetime in your namespace refers to the datetime class, not the module, and can't be used to directly get a handle on the timedelta class.
Removing this line, so that datetime refers to the module, you should be able to access it as datetime.timedelta:
>>> import datetime
>>> dt = datetime.datetime.now()
>>> dt
datetime.datetime(2017, 8, 24, 15, 10, 34, 942209)
>>> dt = dt - datetime.timedelta(hours=24.5)
>>> dt
datetime.datetime(2017, 8, 23, 14, 40, 34, 942209)
Try it here: https://repl.it/KYDN/0
Alternatively, you could import both classes and refer to them directly:
>>> from datetime import datetime, timedelta
>>> dt = datetime.now()
>>> dt
datetime.datetime(2017, 8, 24, 15, 14, 46, 340878)
>>> dt = dt - timedelta(hours=24.5)
>>> dt
datetime.datetime(2017, 8, 23, 14, 44, 46, 340878)
I have data like this: "2016-10-17 09:34:02" with the format: "%Y-%m-%d %H:%M:%S" and I use: from datetime import datetime
My variable is like this:
date_object = datetime.strptime(datetemp, format)
So far, so good...
But I need get only the time part from this object (date_object) to make some comparisons...
When I do this:
print(date_object.time)
I get the following error:
built-in method time of datetime.datetime object at 0x00A94AE8
How can i get just the time part from the object date_object? This will allow me to make comparisons within hourly ranges.
You need to add parenthesis () to your print(date_object.time) call:
print(date_object.time())
Example output:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.time()
datetime.time(22, 14, 6, 996000)
>>> print(now.time())
22:14:06.996000
>>> import datetime as dt
>>> timenow = dt.datetime.now()
>>> timenow
datetime.datetime(2016, 11, 23, 9, 59, 54, 291083)
>>> timenow.date()
datetime.date(2016, 11, 23)
>>> timenow.time()
datetime.time(9, 59, 54, 291083)
the outputs are in datetime type.
>>> print(datetime.time())
00:00:00
or you can convert it into a string and print
>>> time_str = str(timenow.time())
>>> time_str
'09:59:54.291083'
date_object.time().__str__() this will give you the format you want
date_object.time().__str__() is equal to `print(date_object.time())`
but you can store it in a var, not just print it in you screen.
What is the difference between the 2 statements:
import datetime
print datetime.datetime.now()
datetime.datetime(2015, 1, 28, 12, 32, 9, 762118)
from datetime import *
>> datetime.time(datetime.now())
datetime.time(12, 33, 3, 693084)
Actually I want to compare TimeField of a django model with the current day's 1 hour less time. My code snippet for the same:
Mymodel.objects.filter(
follow_up_date=datetime.datetime.now().date,
# commented now
# preferred_time__lt=(
# datetime.datetime.now() - datetime.timedelta(hours=1)),
preferred_time__lt=(datetime.time(datetime.now()) - datetime.timedelta(hours=1)),
)
Mymodel:
class Mymodel(models.Model):
follow_up_date = models.DateField("Follow up Date",null=True,blank=True)
preferred_time = models.TimeField(_("Preferred time"),default=now,
null=True,blank=True)
I am trying to extract all the instances which are scheduled for the day, whose preferred time has elapsed just 1 hour back. Which should be the correct filter for the 'preferred_time'? I got wrong results for the commented code. I am not clear.
This is a cron job i.e management command to be run every 1 hour in django
In the first instance:
datetime.datetime(2015, 1, 28, 12, 32, 9, 762118)
You have a datetime object. It has both the date (first three numbers) and the time (last four numbers).
The second object you mention:
datetime.time(12, 33, 3, 693084)
This is just the time component.
To compare a TimeField, you need just the time component; for a DateField, just the date component.
In your code, you have the following datetime.datetime.now().date this is just the name of the built-in function date. You need to call it:
>>> datetime.datetime.now().date
<built-in method date of datetime.datetime object at 0xb74ac9b0>
>>> datetime.datetime.now().date()
datetime.date(2015, 1, 28)
You also cannot do datetime.time(datetime.datetime.now()), datetime.time() is a constructor method, it is not a way to covert other objects.
You also cannot subtract timedelta from a time object:
To get the correct result, you need to subtract one hour from the datetime object, then convert it to time:
>>> (datetime.datetime.now() - datetime.timedelta(hours=1)).time()
datetime.time(9, 27, 16, 93746)
In the end, your filter would look like:
filter_date = datetime.datetime.now().date()
filter_time = (datetime.datetime.now() - datetime.timedelta(hours=1)).time()
Mymodel.objects.filter(follow_up_date=filter_date,
preferred_time__lt=filter_time)
datetime.now() given date and time information.
datetime.time() give only time information.
e.g
>>> from datetime import *
>>> datetime.now()
datetime.datetime(2015, 1, 28, 12, 52, 35, 164500)
>>> datetime.time(datetime.now())
datetime.time(12, 52, 41, 97521)
>>>
I have a case in my Django :
Variable 't' get data from database. I print 't' variable and the result is 'datetime.time(16, 59, 59)'. It means at 16:59:59.
I want to combine today's date with these time. Does anyone know?
The result that I want is (for example) : 'datetime.datetime(2014, 10, 22, 16, 59, 59)' which is combine from today's date is '2014-10-22' and specified time like '16:59:59'.
Thank you.
Use datetime.combine:
from datetime import datetime, date, time
datetime.combine(date(2014, 10, 22), time(16, 59, 59)).
its what that datetime.combine is for and date.today() returns todays date :
>>> from datetime import datetime ,date , time
>>> datetime.combine(date.today(), time(16, 59, 59))
datetime.datetime(2014, 10, 22, 16, 59, 59)
I am using db.time property to save time required for a conversion:
my_model.conversion_time = time_taken = datetime.datetime.strptime(str(conversion_end - conversion_start), "%H:%M:%S.%f").time()
but when i see the data in datastore viewer , it is stored as datetime object with date of 1970-01-01. Does anybody know how I can just save the time in the datastore
I'm not sure why you have two inline assignments, but to get time out of a timedelta object:
>>> b
datetime.datetime(2013, 7, 15, 10, 21, 31, 599402)
>>> a
datetime.datetime(2013, 7, 15, 10, 18, 11, 251477)
>>> str(b-a)
'0:03:20.347925'
>>> (datetime.datetime.min + (b-a)).time()
datetime.time(0, 3, 20, 347925)
In order to store only the time, you need to use TimeProperty in your datastore. It will be represented internally as datetime, but will store datetime.time() objects.