Datetime--Python的基本日期时间类型
最近在学习Google App Engine日期时间属性时,尝试对Micolog这个基于Google App Engine的Blog后台的评论做了一个简单的修改页面,这样就可以使我们省下了去App Engine的后台去搜寻相关数据库再做修改的时间。编辑代码的过程倒是基本简单,新建一个编辑评论的页面(comment.html),再到admin.py中加入相关代码就可以了。不过,这个过程中倒是遇到一个小问题,python提交时间更新时会抛出一个异常来:
BadValueError: Property date must be a datetime
解决办法倒是很简单,将时间格式化成Datetime类型即可。
比如说:我在模板中对时间格式化为{{comment.date|datetz:"Y-m-d H:i:s"}}(如:2010-07-05-20:00:12这样的形式),那么在程序代码中可以这样写做:
comment.date=datetime.strptime(commentdate,"%Y-%m-%d %H:%M:%S")
当然其他类型的格式化都是可取的。由于Python中对时间进行格式化的相关字符与Django模板中time过滤器所用的格式字符串略有区别,下面就分别对其进行简单介绍,希望对你有所帮助。
| 格式字符串 |
描述 | 示例输出 |
|---|---|---|
| a | 'a.m.' 或者 'p.m.' 。(这与PHP中的输出略有不同,因为为了匹配美联社风格,它包含了句点。 | 'a.m.' |
| A | 'AM' 或者 'PM' 。 | 'AM' |
| b | 月份,文字式的,三个字母,小写。 | 'jan' |
| d | 一月的第几天,两位数字,带前导零。 | '01' 到 '31' |
| D | 一周的第几天,文字式的,三个字母。 | 'Fri' |
| f | 时间,12小时制的小时和分钟数,如果分钟数为零则不显示。 | '1' , '1:30' |
| F | 月份,文字式的,全名。 | 'January' |
| g | 小时,12小时制,没有前导零。 | '1' 到 '12' |
| G | 小时,24小时制,没有前导零。 | '0' 到 '23' |
| h | 小时,12小时制。 | '01' 到 '12' |
| H | 小时,24小时制。 | '00' 到 '23' |
| i | 分钟。 | '00' 到 '59' |
| j | 一月的第几天,不带前导零。 | '1' 到 '31' |
| l | 一周的第几天,文字式的,全名。 | 'Friday' |
| L | 是否为闰年的布尔值。 | True 到 False |
| m | 月份,两位数字,带前导零。 | '01' 到 '12' |
| M | 月份,文字式的,三个字母。 | 'Jan' |
| n | 月份,没有前导零。 | '1' 到 '12' |
| N | 美联社风格的月份缩写。 | 'Jan.' , 'Feb.' , 'March' , 'May' |
| O | 与格林威治标准时间的时间差(以小时计)。 | '+0200' |
| P | 时间,12小时制的小时分钟数以及a.m./p.m.,分钟数如果为零则不显示,用字符串表示特殊时间点,如 'midnight' 和 'noon' 。 | '1 a.m.' , '1:30 p.m.' , 'midnight' , 'noon' , '12:30 p.m.' |
| r | RFC 822 格式的日期。 | 'Thu, 21 Dec 2000 16:01:07 +0200' |
| s | 秒数,两位数字,带前导零。 | '00' 到 '59' |
| S | 英语序数后缀,用于表示一个月的第几天,两个字母。 | 'st' , 'nd' , 'rd' 到 'th' |
| t | 指定月份的天数。 | 28 到 31 |
| T | 本机的时区。 | 'EST' , 'MDT' |
| w | 一周的第几天,数字,带前导零。 | '0' (Sunday) 到 '6' (Saturday) |
| W | ISO-8601 一年中的第几周,一周从星期一开始。 | 1 , 23 |
| y | 年份,两位数字。 | '99' |
| Y | 年份,四位数字。 | '1999' |
| z | 一年的第几天。 | 0 到 365 |
| Z | 以秒计的时区偏移量,这个偏移量对于UTC西部时区总是负数,对于UTC东部时区总是正数。 | -43200 到 43200 |
strftime() and strptime() Behavior
date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string. Broadly speaking, d.strftime(fmt) acts like the time module’s time.strftime(fmt, d.timetuple()) although not all objects support a timetuple() method.
Conversely, the datetime.strptime() class method creates a datetime object from a string representing a date and time and a corresponding format string. datetime.strptime(date_string, format) is equivalent to datetime(*(time.strptime(date_string, format)[0:6])).
For time objects, the format codes for year, month, and day should not be used, as time objects have no such values. If they’re used anyway, 1900 is substituted for the year, and 1 for the month and day.
For date objects, the format codes for hours, minutes, seconds, and microseconds should not be used, as date objects have no such values. If they’re used anyway, 0 is substituted for them.
New in version 2.6: time and datetime objects support a %f format code which expands to the number of microseconds in the object, zero-padded on the left to six places.
For a naive object, the %z and %Z format codes are replaced by empty strings.
For an aware object:
%zutcoffset()is transformed into a 5-character string of the form +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM is a 2-digit string giving the number of UTC offset minutes. For example, ifutcoffset()returnstimedelta(hours=-3, minutes=-30),%zis replaced with the string'-0330'.%Z- If
tzname()returnsNone,%Zis replaced by an empty string. Otherwise%Zis replaced by the returned value, which must be a string.
The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common.
The following is a list of all the format codes that the C standard (1989 version) requires, and these work on all platforms with a standard C implementation. Note that the 1999 version of the C standard added additional format codes.
The exact range of years for which strftime() works also varies across platforms. Regardless of platform, years before 1900 cannot be used.
| Directive |
Meaning | Notes |
|---|---|---|
%a |
Locale’s abbreviated weekday name. | |
%A |
Locale’s full weekday name. | |
%b |
Locale’s abbreviated month name. | |
%B |
Locale’s full month name. | |
%c |
Locale’s appropriate date and time representation. | |
%d |
Day of the month as a decimal number [01,31]. | |
%f |
Microsecond as a decimal number [0,999999], zero-padded on the left | (1) |
%H |
Hour (24-hour clock) as a decimal number [00,23]. | |
%I |
Hour (12-hour clock) as a decimal number [01,12]. | |
%j |
Day of the year as a decimal number [001,366]. | |
%m |
Month as a decimal number [01,12]. | |
%M |
Minute as a decimal number [00,59]. | |
%p |
Locale’s equivalent of either AM or PM. | (2) |
%S |
Second as a decimal number [00,61]. | (3) |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (4) |
%w |
Weekday as a decimal number [0(Sunday),6]. | |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (4) |
%x |
Locale’s appropriate date representation. | |
%X |
Locale’s appropriate time representation. | |
%y |
Year without century as a decimal number [00,99]. | |
%Y |
Year with century as a decimal number. | |
%z |
UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). | (5) |
%Z |
Time zone name (empty string if the object is naive). | |
%% |
A literal '%' character. |
Notes:
- When used with the
strptime()method, the%fdirective accepts from one to six digits and zero pads on the right.%fis an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available). - When used with the
strptime()method, the%pdirective only affects the output hour field if the%Idirective is used to parse the hour. - The range really is
0to61; according to the Posix standard this accounts for leap seconds and the (very rare) double leap seconds. Thetimemodule may produce and does accept leap seconds since it is based on the Posix standard, but thedatetimemodule does not accept leap seconds instrptime()input nor will it produce them instrftime()output. - When used with the
strptime()method,%Uand%Ware only used in calculations when the day of the week and the year are specified. - For example, if
utcoffset()returnstimedelta(hours=-3, minutes=-30),%zis replaced with the string'-0330'.
本文永久链接 http://www.tangblog.info/2010/07/5/python-datetime-basic-date-and-time-types.html

“Datetime--Python的基本日期时间类型”共有 39 条留言
呵呵,没事就折腾啊
是倒是阿,可我也不太清楚邮件发送情况哦,后台记录忘加递送状态信息了……
呃,GAE和Micolog的邮局不是很好整啊
发送邮件是系统自动发送的,我也不清楚是咋回事
使用的是CSS3效果IE8以下的浏览器是不支持的,虽然我平时使用的最多的是IE8,但毕竟有人使用其他浏览器,所以就把这个加进来了
我是在pps上看的,你这回复的圆角边框在ie下怎么看不出来
我感觉那个还不错啊,前天我才把720P高清全集下载下来,现在我都看完19集了
呵呵,我只是加上了CSS3圆角效果
现在倒是好了,不过你的主题在IE下都跑到最左边了
呵呵,换了没多久的
随便改改,看看别人比较好看的,就拿来用用
先这样将就一下,有空再弄弄……
我把我导航栏的首页那个小房子给去掉了!
你需要新建一个文件,比如说:menubar.js,然后把这些内容拷贝进去就可以了,当然前提是你的网站需要加载jquery.js,你现在的没有加载
base.html中的<script type="text/javascript" src="/static/js/jquery-1.2.6.min.js"></script>
修改成<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>就可以了
我去看看。
给你找了一个这个代码的初始地址,里面有source code 和Demo 你应该可以很快就看懂的:
http://jqueryglobe.com/article/animated-menu
上面的代码是我在作者原代码上稍作修改了的,因为在IE6下元素的overflow:hidden;好像不太支持,导致重复显示,我就让这段代码在IE6下不运行了,你可以参考两者改哦,CSS你可以直接使用我的就行啦
导航栏是使用js加css做的,你可以看看这段代码:
var browser=navigator.appName
var b_version=navigator.appVersion
var version=b_version.split(";");
var trim_Version=version[1].replace(/[ ]/g,"");
if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE6.0"){}
else {
$("#navigation li a").wrapInner( '' ).append( '' );
$("#navigation li a").each(function() {
$( '' + $(this).text() + '' ).appendTo( this );
});
$("#navigation li a").hover(function() {
$(".out",this).stop().animate({'top':'45px'},250);
$(".over",this).stop().animate({'top':'0px'},250);
$(".bg",this).stop().animate({'top':'-5px'},130);
}, function() {
$(".out",this).stop().animate({'top':'0px'}, 250);
$(".over",this).stop().animate({'top':'-45px'}, 250);
$(".bg",this).stop().animate({'top':'-45px'},130);
});}