160
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.
68
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
- [Django]-How to validate an Email address in Django?
- [Django]-IntegrityError duplicate key value violates unique constraint β django/postgres
- [Django]-Django testing: Test the initial value of a form field
33
import datetime
datetime.date.today() # Returns 2018-01-15
datetime.datetime.now() # Returns 2018-01-15 09:00
- [Django]-Adding django admin permissions in a migration: Permission matching query does not exist
- [Django]-How to submit form without refreshing page using Django, Ajax, jQuery?
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
10
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.
- [Django]-Django β Website Home Page
- [Django]-What is the best location to put templates in django project?
- [Django]-Loading initial data with Django 1.7+ and data migrations
8
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
- [Django]-Django order_by query set, ascending and descending
- [Django]-Django project models.py versus app models.py
- [Django]-Django models: mutual references between two classes and impossibility to use forward declaration in python
1
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
- [Django]-Django return file over HttpResponse β file is not served correctly
- [Django]-Manager isn't accessible via model instances
- [Django]-Disabled field is not passed through β workaround needed