在Django中更改日期格式
我正在尝试使用此事件日历(在Django中),它以以下格式保存和显示日期:
I'me trying to use this eventCalendar in Django, which saves and shows dates in this format:
2012-02-27T13:15:00.000+10:00
但是当我将事件保存在数据库中时,事件以以下格式保存:
but when I save events in the database, they're saved in this format:
Mon Feb 27 2012 13:15:00 GMT+0330 (Iraq Standard Time)
因此由于这种格式,数据库中的事件将不会出现在日历上.如何转换这种格式?我尝试过这样的事情:
so events from the database won't appear on the calendar because of this format. How can I convert this format? I tried some thing like this:
datetime.strptime(mydatetime, "%Y-%m-%dT%H:%M:%S+0000")
但是我反复遇到这样的错误:
but I'm repeatedly getting errors like this:
'module' object has no attribute 'strptime'
日期为字符串格式
strptime
用于将字符串解析为日期时间对象.格式字符串表示如何解析字符串, not 则是您希望日期时间在以后打印为字符串时采用的格式.因此,首先需要使格式字符串与输入字符串的日期格式匹配.
strptime
is used to parse a string into a datetime object. The format string indicates how to parse the string, not the format you want the datetime to take when later printed as a string. So first off you need to make the format string match the date format of the input string.
一旦您从 strptime
获得了日期时间,就可以将 strftime
与当前的格式字符串一起使用,以将其显示在所需的显示中.
Once you've gotten a datetime from strptime
, you can then use strftime
with your current format string to get it into the display you want.
尽管如此,看来您的进口商品还是有问题.该错误似乎表明您已经完成:
That said, though, it appears you've got a problem with your imports. The error seems to indicate that you've done:
import datetime
datetime.strptime(...)
那是不对的. strptime
和 strftime
是 datetime.datetime
的关闭方法,因此您需要像以下那样修改导入:
That's incorrect. strptime
and strftime
are methods off datetime.datetime
, so you need to either modify your import like:
from datetime import datetime
或者,像下面这样修改对 strptime
的调用:
Or, modify your call to strptime
like:
datetime.datetime.strptime(...)
更新
您将使用诸如 Mon Feb 27 2012 Mon 2月27日格林尼治标准时间+0330(伊拉克标准时间)
之类的字符串开始.Python非常出色,但并非无所不知;如果要将其转换为 datetime
,则必须告诉它 how .这就是传递给 strptime
的格式字符串的目的.您需要创建一个格式字符串,该格式字符串表示数据库中表示的当前字符串日期和时间(练习留给读者).反过来考虑,您想要实际代表这样一个 datetime
这样的思路,您会怎么做.
You're starting off with a string like Mon Feb 27 2012 13:15:00 GMT+0330 (Iraq Standard Time)
. Python is pretty awesome, but it's not omniscient; if you want to convert this to a datetime
you have to tell it how. That's the purpose of the format string you pass to strptime
. You need to create a format string that represents your current string date and time as represented in the database (exercise left to reader). Think in reverse, along the lines of it you wanted to actually represent a datetime
like that, how would you do it.
这将为您提供 datetime
.从那里,您现在可以使用 strftime
将那个 datetime
格式化为字符串,这一次传递您想要的实际格式.
This will net you a datetime
. From there, you can now format that datetime
as a string with strftime
, passing the actual format you want, this time.
所以过程是:
- 从数据库创建一个表示您的 current 字符串的格式字符串
- 使用该格式字符串作为
strptime
的参数来获取datetime
- 创建一个格式字符串,表示您希望日期使用的格式(已经完成)
- 使用该格式字符串作为
strftime
的参数,将datetime
从第2步转换为所需的字符串.
- Create a format string representing your current string from the database
- Use that format string as an argument to
strptime
to get adatetime
- Create a format string representing the format you want the date to be in (already done)
- Use that format string as the argument to
strftime
to convert thedatetime
from step 2 to your desired string.