最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Python中用strftime格式化datetime出错

Python crifan 5245浏览 0评论

折腾:

[已解决]Flask中的templates的html中格式化datetime

期间,用代码:

@app.template_filter(‘datetime_format’)
def _jinja2_filter_datetime_format(datetimeValueformat=‘%M/%d %H:%m’):
    """convert a datetime to a different format."""
    return datetimeValue.strftime(format)

格式化日期时间,得到的值是:

11/24 20:08

不是希望的:

8/24 20:8

去搜:

python strftime

Python time strftime()方法 | 菜鸟教程

http://www.runoob.com/python/att-time-strftime.html

->发现strftime是time才有的方法

-》我此处的是datetime

Python time strptime()方法 | 菜鸟教程

http://www.runoob.com/python/att-time-strptime.html

15.3. time — Time access and conversions — Python 2.7.12 documentation

https://docs.python.org/2/library/time.html

python datetime format

python对时间日期做格式化 – 走到天亮 – 博客园

http://www.cnblogs.com/65702708/archive/2011/04/17/2018936.html

8.1. datetime — Basic date and time types — Python 2.7.12 documentation

https://docs.python.org/2/library/datetime.html

datetime.strftime(format)

Return a string representing the date and time, controlled by an explicit format string. For a complete list of formatting directives, see section strftime() and strptime() Behavior.

"

结果发现:

此处的strftime的格式,是参考:

[已解决]swift格式化日期时间为字符串的完整的格式的语法

以为是:

m=minute

M=Month

所以此处是用:

%M/%d %H:%m

想要输出:

月/日 时:分

但是官网的示例中是:

>>> d.strftime("%d/%m/%y")
’11/03/02′
datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
dt.strftime("%A, %d. %B %Y %I:%M%p")
‘Tuesday, 21. November 2006 04:30PM’

->

此处的是:

M=minute

m=Month

看来:

之前的通用的

[已解决]swift格式化日期时间为字符串的完整的格式的语法

和此处的Python的日期时间的格式化的语法,是不一样的。。。

此处的Python中的日期时间的格式化的语法是:

8.1. datetime — Basic date and time types — Python 2.7.12 documentation

https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

DirectiveMeaningExampleNotes
%aWeekday as locale’s abbreviated name.
Sun, Mon, …, Sat (en_US);
So, Mo, …, Sa (de_DE)
(1)
%AWeekday as locale’s full name.
Sunday, Monday, …, Saturday (en_US);
Sonntag, Montag, …, Samstag (de_DE)
(1)
%wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.0, 1, …, 6 
%dDay of the month as a zero-padded decimal number.01, 02, …, 31 
%bMonth as locale’s abbreviated name.
Jan, Feb, …, Dec (en_US);
Jan, Feb, …, Dez (de_DE)
(1)
%BMonth as locale’s full name.
January, February, …, December (en_US);
Januar, Februar, …, Dezember (de_DE)
(1)
%mMonth as a zero-padded decimal number.01, 02, …, 12 
%yYear without century as a zero-padded decimal number.00, 01, …, 99 
%YYear with century as a decimal number.1970, 1988, 2001, 2013 
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23 
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12 
%pLocale’s equivalent of either AM or PM.
AM, PM (en_US);
am, pm (de_DE)
(1), (2)
%MMinute as a zero-padded decimal number.00, 01, …, 59 
%SSecond as a zero-padded decimal number.00, 01, …, 59(3)
%fMicrosecond as a decimal number, zero-padded on the left.000000, 000001, …, 999999(4)
%zUTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).(empty), +0000, -0400, +1030(5)
%ZTime zone name (empty string if the object is naive).(empty), UTC, EST, CST 
%jDay of the year as a zero-padded decimal number.001, 002, …, 366 
%UWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.00, 01, …, 53(6)
%WWeek number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.00, 01, …, 53(6)
%cLocale’s appropriate date and time representation.
Tue Aug 16 21:30:00 1988 (en_US);
Di 16 Aug 21:30:00 1988 (de_DE)
(1)
%xLocale’s appropriate date representation.
08/16/88 (None);
08/16/1988 (en_US);
16.08.1988 (de_DE)
(1)
%XLocale’s appropriate time representation.
21:30:00 (en_US);
21:30:00 (de_DE)
(1)
%%A literal '%' character.% 

所以,此处改为:

@app.template_filter(‘datetime_format’)
# def _jinja2_filter_datetime_format(datetimeValue, format=’%M/%d %H:%m’):
def _jinja2_filter_datetime_format(datetimeValueformat=‘%m/%d %H:%M’):
    """convert a datetime to a different format."""
    return datetimeValue.strftime(format)

即可输出:

08/24 20:11

了。

[总结]

Python中的datetime(以及date,time),在涉及到用strptime去parse解析字符串为变量,或用strftime去format格式化输出的时候,对应的语法是:

8.1. datetime — Basic date and time types — Python 2.7.12 documentation

https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

其中需要注意的是:

1.其和之前别的地方总结的

关于日期时间的格式化的通用的语法:

[已解决]swift格式化日期时间为字符串的完整的格式的语法

有点不一样:

Python中是:

%M=minute=分钟

%m=month=月份

而其它通用的语法是:

%M=month=月份

%m=minute=分钟

2.且Python中的datetime的strftime的语法:

%d    Day of the month as a zero-padded decimal number.    01, 02, …, 31
%m    Month as a zero-padded decimal number.    01, 02, …, 12
%H    Hour (24-hour clock) as a zero-padded decimal number.    00, 01, …, 23
%M    Minute as a zero-padded decimal number.    00, 01, …, 59

此处,不支持:

月份 日期 小时 分钟 

的如果是有一位数,就输出一位数

而是:

如果是一位数,比如8,也是前面补0,显示两位数的08

而其它很多地方,是支持的,不带补0,在只有1位时只显示1位的。

转载请注明:在路上 » [已解决]Python中用strftime格式化datetime出错

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.197 seconds, using 22.02MB memory