【问题标题】:How can I get only the hour or minute part from post request in Django?如何从 Django 中的发布请求中仅获取小时或分钟部分?
【发布时间】:2020-08-18 02:02:38
【问题描述】:

我只想在 django 中接收小时部分或分钟部分。现在我正在使用remindTime = request.POST.get("remindTime") 来获取时间,但我只想要小时或分钟或天或月。我该怎么做?

这是模型

class Reminder(models.Model):
    remindTime = models.DateTimeField(auto_now_add=False, auto_now=False)

我如何获得时间

if request.method == "POST":
    remindTime = request.POST.get("remindTime")

泰!

【问题讨论】:

  • remindTime转换为python日期时间对象

标签: python django


【解决方案1】:

首先,将您的字符串对象转换为日期时间对象:

my_string = '2019-10-31'

# Create date object in given time format yyyy-mm-dd
my_date = datetime.strptime(my_string, "%Y-%m-%d")

print(my_date)
print('Type: ',type(my_date))

输出:

2019-10-31 00:00:00 Type:  

第二步,获取你的小时和分钟:

print('Month: ', my_date.month) # To Get month from date
print('Year: ', my_date.year) # To Get month from year

输出:

Month:  10 Year:  2019

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多