简体   繁体   English

如何仅在 Django 中分别获取当前日期和当前时间?

[英]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. Django 决定使用在formats.py文件中定义的DATETIME_INPUT_FORMATS Which makes sense, because I am passing in a datetime.now() to both fields.这是有道理的,因为我将datetime.now()传递给两个字段。

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.我想我可以让 Django 分别使用DATE_INPUT_FORMATSTIME_INPUT_FORMATS ,如果我只传入当前日期和当前时间。

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?有没有不同的方法来实现这一目标?

For the date, you can use datetime.date.today() or datetime.datetime.now().date() .对于日期,您可以使用datetime.date.today()datetime.datetime.now().date()

For the time, you can use datetime.datetime.now().time() .对于时间,您可以使用datetime.datetime.now().time()


However, why have separate fields for these in the first place?但是,为什么首先要为这些设置单独的字段? Why not use a single DateTimeField ?为什么不使用单个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.如果您只需要一个或另一个,您总是可以在模型上定义辅助函数,稍后返回.date().time()

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
import datetime

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

datetime.datetime.now() # Returns 2018-01-15 09:00
 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

Another way to get datetime UTC with milliseconds.另一种以毫秒为单位获取日期时间 UTC 的方法。

from datetime import datetime

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

2020-10-29T14:46:37.655Z

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在 django 中,对 datetime 字段使用timezone.now() ,因为 django 支持timezone ,它只返回基于USE TZ设置的日期时间,或者只是 timezone 'aware' datetime 对象

For a reference, I've got TIME_ZONE = 'Asia/Kolkata' and USE_TZ = True ,作为参考,我有TIME_ZONE = 'Asia/Kolkata'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 文档中的 时区设置国际化和本地化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM