Django字符串到日期格式 [英] Django string to date format

查看:298
本文介绍了Django字符串到日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的字符串日期转换为django日期格式。我试过一个方法。但没有工作。

  date = datetime.datetime.strptime(request.POST.get('date'),Y-mm-dd ).date()

我收到这个错误。

 时间数据2014-04-07与格式不一致Y-mm-dd

我的代码有什么问题。

解决方案

应该是%Y-%m-%d

 code>>>> s =2014-04-07
>>> datetime.datetime.strptime(s,%Y-%m-%d)。date()
datetime.date(2014,4,7)
pre>

根据文档




  • %Y 年份为十进制数字

  • %m - 作为零填充十进制数的月份

  • %d - 零填充十进制数的一个月的日期



希望有帮助。


I want to convert my string date to django date format. I tried a method. but did not work.

date = datetime.datetime.strptime(request.POST.get('date'),"Y-mm-dd").date()

I got this error.

time data '2014-04-07' does not match format 'Y-mm-dd'

what 's wrong in my code.

解决方案

It should be %Y-%m-%d:

>>> s = "2014-04-07"
>>> datetime.datetime.strptime(s, "%Y-%m-%d").date()
datetime.date(2014, 4, 7)

According to the documentation:

  • %Y stands for a year with century as a decimal number
  • %m - month as a zero-padded decimal number
  • %d - day of the month as a zero-padded decimal number

Hope that helps.

这篇关于Django字符串到日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆