Python strftime - 不带前导0的日期?
当使用Python strftime
时,如果日期在10号之前,有没有办法删除日期的前0,即。 那么 01
是 1
吗? 找不到%
的东西吗?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当使用Python strftime
时,如果日期在10号之前,有没有办法删除日期的前0,即。 那么 01
是 1
吗? 找不到%
的东西吗?
谢谢!
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(22)
因为Python实际上只是在您的平台上调用C语言
strftime(3)
函数,所以可能有一些格式字符可以用来控制前导零; 尝试man strftime
看看。 但是,当然,结果是不可移植的,Python 手册会提醒你。 :-)我会尝试使用新型
datetime
对象,它具有诸如t.year
和t.month
和等属性>t.day
,并将它们置于%
运算符的正常、高性能格式化中,该运算符确实支持对前导零的控制。 有关详细信息,请参阅 http://docs.python.org/library/datetime.html 。 更好的是,如果您的 Python 有"".format()
运算符,那么它会更现代; 它还有很多数字格式选项。 请参阅: http://docs.python.org/library/string.html#字符串格式。Because Python really just calls the C language
strftime(3)
function on your platform, it might be that there are format characters you could use to control the leading zero; tryman strftime
and take a look. But, of course, the result will not be portable, as the Python manual will remind you. :-)I would try using a new-style
datetime
object instead, which has attributes liket.year
andt.month
andt.day
, and put those through the normal, high-powered formatting of the%
operator, which does support control of leading zeros. See http://docs.python.org/library/datetime.html for details. Better yet, use the"".format()
operator if your Python has it and be even more modern; it has lots of format options for numbers as well. See: http://docs.python.org/library/string.html#string-formatting.根据 Alex 的方法,这适用于字符串开头和空格后的情况:
我比 .format 或 %-d 更喜欢这个,因为这是跨平台的,并且允许我继续使用 strftime (来获取内容)例如“十一月”和“星期一”)。
Based on Alex's method, this will work for both the start-of-string and after-spaces cases:
I like this better than .format or %-d because this is cross-platform and allows me to keep using strftime (to get things like "November" and "Monday").
老问题,但是 %l (小写 L)在 strftime 中对我有用:但这可能并不适合所有人,因为它没有在我发现的 Python 文档中列出
Old question, but %l (lower-case L) worked for me in strftime: this may not work for everyone, though, as it's not listed in the Python documentation I found
我迟到了,但是一个简单的列表切片就可以完成工作
I am late, but a simple list slicing will do the work
标准库对于大多数情况来说已经足够好了,但是对于真正详细的日期操作,您应该始终寻找一些专门的第三方库。
使用箭头:
查看完整的支持的令牌列表。
The standard library is good enough for most cases but for a really detailed manipulation with dates you should always look for some specialized third-party library.
Using Arrow:
Look at the full list of supported tokens.
有点棘手,但对我
前任有用。 从
2021-02-01T00:00:00.000Z
到2021-02-1
A little bit tricky but works for me
ex. from
2021-02-01T00:00:00.000Z
to2021-02-1
业余方法删除 Day & 的“0”前缀 月份,通过转换为“int”
An amateur approach to remove '0' prefix for Day & Month, by casting to 'int'
实际上我也遇到了同样的问题,我意识到,如果在
%
和字母之间添加连字符,则可以删除前导零。例如
%Y/%-m/%-d
:这只适用于 Unix(Linux、OS X),不适用于 Windows(包括 Cygwin)。 在 Windows 上,您可以使用
#
,例如%Y/%#m/%#d
。Actually I had the same problem and I realized that, if you add a hyphen between the
%
and the letter, you can remove the leading zero.For example
%Y/%-m/%-d
:This only works on Unix (Linux, OS X), not Windows (including Cygwin). On Windows, you would use
#
, e.g.%Y/%#m/%#d
.随着
格式
自 python2.6 以来的方法:
虽然可能超出了原始问题的范围,但对于更有趣的格式,您可以执行以下操作:
从 python3.6 开始,这可以表示为 内联格式化字符串:
We can do this sort of thing with the advent of the
format
method since python2.6:Though perhaps beyond the scope of the original question, for more interesting formats, you can do stuff like:
And as of python3.6, this can be expressed as an inline formatted string:
根据 % 和字母之间的宽度和精度规范(例如表示月份中的“d”) /time.html" rel="noreferrer">http://docs.python.org/library/time.html ——但这绝对是一个不可移植的解决方案(例如,在我的 Mac 上不起作用; -)。 也许您可以在
strftime
之后使用字符串替换(或 RE,对于非常讨厌的格式)来解决这个问题? 例如:Some platforms may support width and precision specification between
%
and the letter (such as 'd' for day of month), according to http://docs.python.org/library/time.html -- but it's definitely a non-portable solution (e.g. doesn't work on my Mac;-). Maybe you can use a string replace (or RE, for really nasty format) after thestrftime
to remedy that? e.g.:这里是GNU C 库中
strftime()
支持的修饰符的文档。 (就像人们之前说的,它可能不可移植。)您可能感兴趣的是:%e
而不是%d
将用一个space与 Python 文档
进行比较strftime() 格式代码
。%d
已记录:但是
%e
没有记录。 尽管没有记录它,但它似乎确实对我有用(运行 Linux):我不知道它是否适用于您的操作系统。
Here is the documentation of the modifiers supported by
strftime()
in the GNU C library. (Like people said before, it might not be portable.) Of interest to you might be:%e
instead of%d
will replace leading zero in day of month with a spaceCompare with the Python documentation of
strftime() Format Codes
.%d
is documented:But
%e
is not documented. Even though it is not documented, it does seem to work for me regardless (running Linux):I don't know if it will work on your operating system.
聚会已经很晚了,但
%-d
对我来说很有效。datetime.now().strftime('%B %-d, %Y')
产生类似于 "November 5, 2014"欢呼 :)
quite late to the party but
%-d
works on my end.datetime.now().strftime('%B %-d, %Y')
produces something like "November 5, 2014"cheers :)
在 Windows 上,添加“#”,如“%#m/%#d/%Y %#I:%M:%S %p”
参考:https://msdn.microsoft.com/en-us/library/fe06s4ak.aspx
On Windows, add a '#', as in '%#m/%#d/%Y %#I:%M:%S %p'
For reference: https://msdn.microsoft.com/en-us/library/fe06s4ak.aspx
看看下面的
-
:Take a look at
-
bellow:我发现 Django 模板日期格式过滤器既快速又简单。 它去掉了前导零。 如果您不介意导入 Django 模块,请检查一下。
http://docs.djangoproject.com/en/dev/ref/模板/内置/#date
I find the Django template date formatting filter to be quick and easy. It strips out leading zeros. If you don't mind importing the Django module, check it out.
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#date
只需使用
replace
即可,如下所示:(datetime.date.now()).strftime("%Y/%m/%d").replace("/0", "/" )
它将输出:
simply use
replace
like this:(datetime.date.now()).strftime("%Y/%m/%d").replace("/0", "/")
it will output:
对于
%d
,您可以使用int()
转换为整数,然后它会自动删除前导0并变成整数。 然后,您可以使用str()
将其转换回字符串。For
%d
you can convert to integer usingint()
then it'll automatically remove leading 0 and becomes integer. You can then convert back to string usingstr()
.例如,即使在同一操作系统的不同版本之间,使用“%-d”也是不可移植的。
更好的解决方案是单独提取日期组件,并在特定于日期的格式化运算符和每个组件的日期属性访问之间进行选择。
using, for example, "%-d" is not portable even between different versions of the same OS.
A better solution would be to extract the date components individually, and choose between date specific formatting operators and date attribute access for each component.
如果我们只想获取不带前导零的日期,我们可以
if we want to fetch only date without leading zero we can
Python 3.6+:
Python 3.6+: