How do I get the current date and current time only respectively in Django?

PythonDjangoDjango Models

Python Problem Overview


I came across an interesting situation when using this class:

class Company(models.Model):
    date = models.DateField()
    time = models.TimeField()

c = Company(date=datetime.datetime.now(), time=datetime.datetime.now()) 

Django decides to use DATETIME_INPUT_FORMATS defined within the formats.py file. Which makes sense, because I am passing in a datetime.now() to both fields.

I think I could make Django to use DATE_INPUT_FORMATS and TIME_INPUT_FORMATS respectively, if I passed in only the current date and current time in.

Something like this:

c = Company(date=datetime.date.now(), time=datetime.time.now()) 

But this obviously throws an exception as now doesn't exist like that. Is there a different way to achieve this?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

Solution 3 - Python

import datetime

datetime.date.today()  # Returns 2018-01-15

datetime.datetime.now() # Returns 2018-01-15 09:00

Solution 4 - Python

 import datetime

Current Date and time

     print(datetime.datetime.now())
     #2019-09-08 09:12:12.473393

Current date only

     print(datetime.date.today())
     #2019-09-08

Current year only

     print(datetime.date.today().year)
     #2019

Current month only

     print(datetime.date.today().month)
     #9

Current day only

     print(datetime.date.today().day)
     #8

Solution 5 - Python

Another way to get datetime UTC with milliseconds.

from datetime import datetime

datetime.utcnow().isoformat(sep='T', timespec='milliseconds') + 'Z'

2020-10-29T14:46:37.655Z

Solution 6 - Python

A related info, to the question...

In django, use timezone.now() for the datetime field, as django supports timezone, it just returns datetime based on the USE TZ settings, or simply timezone 'aware' datetime objects

For a reference, I've got TIME_ZONE = 'Asia/Kolkata' and USE_TZ = True,

from django.utils import timezone
import datetime

print(timezone.now())  # The UTC time
print(timezone.localtime())  # timezone specified time, 
print(datetime.datetime.now())  # default local time

# output
2020-12-11 09:13:32.430605+00:00
2020-12-11 14:43:32.430605+05:30  # IST is UTC+5:30
2020-12-11 14:43:32.510659

refer timezone settings and Internationalization and localization in django docs for more details.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHoumanView Question on Stackoverflow
Solution 1 - PythonAmberView Answer on Stackoverflow
Solution 2 - PythonAbdul RazakView Answer on Stackoverflow
Solution 3 - PythonEthan BrimhallView Answer on Stackoverflow
Solution 4 - PythonMuhammad Faizan FareedView Answer on Stackoverflow
Solution 5 - PythonBruno WegoView Answer on Stackoverflow
Solution 6 - PythonAkshay ChandranView Answer on Stackoverflow