How do I get the current date and current time only respectively in Django?
For the date, you can use datetime.date.today()
or datetime.datetime.now().date()
.
For the time, you can use datetime.datetime.now().time()
.
However, why have separate fields for these in the first place? Why not use a single DateTimeField
?
You can always define helper functions on the model that return the .date()
or .time()
later if you only want one or the other.
import datetime
datetime.datetime.now().strftime ("%Y%m%d")
20151015
For the time
from time import gmtime, strftime
showtime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
print showtime
2015-10-15 07:49:18