Houman
Houman

Reputation: 66390

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

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?

Upvotes: 90

Views: 209000

Answers (7)

Mohammad Ardestani
Mohammad Ardestani

Reputation: 3

in models, you can use DateTimeField and set auto_now_add=True

class Company(models.Model):
    date = models.DateTimeField(auto_now_add=True)

Upvotes: 0

Akshay Chandran
Akshay Chandran

Reputation: 1383

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.

Upvotes: 15

Bruno Wego
Bruno Wego

Reputation: 2450

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

Upvotes: 1

Muhammad Faizan Fareed
Muhammad Faizan Fareed

Reputation: 3758

 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

Upvotes: 10

Ethan Brimhall
Ethan Brimhall

Reputation: 376

import datetime

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

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

Upvotes: 35

Abdul Razak
Abdul Razak

Reputation: 2804

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

Upvotes: 68

Amber
Amber

Reputation: 527378

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.

Upvotes: 163

Related Questions