从Python日期中提取两位数的月份和日期[重复]

时间:2022-08-25 18:59:39

This question already has an answer here:

这个问题在这里已有答案:

Is there a way to extract month and day using isoformats? Lets assume today's date is March 8, 2013.

有没有办法使用isoformats提取月和日?让我们假设今天的日期是2013年3月8日。

>>> d = datetime.date.today()
>>> d.month
3
>>> d.day
8

I want:

我想要:

>>> d = datetime.date.today()
>>> d.month
03
>>> d.day
08

I can do this by writing if statements and concatenating a leading 0 in case the day or month is a single digit but was wondering whether there was an automatic way of generating what I want.

我可以通过编写if语句和连接前导0来实现这一点,以防日期或月份是单个数字但是想知道是否有自动生成我想要的方法。

2 个解决方案

#1


108  

Look at the types of those properties:

看看这些属性的类型:

In [1]: import datetime

In [2]: d = datetime.date.today()

In [3]: type(d.month)
Out[3]: <type 'int'>

In [4]: type(d.day)
Out[4]: <type 'int'>

Both are integers. So there is no automatic way to do what you want. So in the narrow sense, the answer to your question is no.

两者都是整数。所以没有自动的方法来做你想要的。所以从狭义上讲,你的问题的答案是否定的。

If you want leading zeroes, you'll have to format them one way or another. For that you have several options:

如果你想要前导零,你将不得不以这种或那种方式格式化它们。为此你有几个选择:

In [5]: '{:02d}'.format(d.month)
Out[5]: '03'

In [6]: '%02d' % d.month
Out[6]: '03'

In [7]: d.strftime('%m')
Out[7]: '03'

#2


10  

you can use a string formatter to pad any integer with zeros. It acts just like C's printf.

您可以使用字符串格式化程序用零填充任何整数。它就像C的printf一样。

>>> d = datetime.date.today()
>>> '%02d' % d.month
'03'

Updated for py36: Use f-strings! For general ints you can use the d formatter and explicitly tell it to pad with zeros:

针对py36更新:使用f-strings!对于一般的整数,您可以使用d格式化程序并明确告诉它用零填充:

 >>> d = datetime.date.today()
 >>> f"{d.month:02d}"
 '07'

But datetimes are special and come with special formatters that are already zero padded:

但是,日期时间是特殊的,并且带有已填零的特殊格式化程序:

 >>> f"{d:%d}"  # the day
 '01'
 >>> f"{d:%m}"  # the month
 '07'

#1


108  

Look at the types of those properties:

看看这些属性的类型:

In [1]: import datetime

In [2]: d = datetime.date.today()

In [3]: type(d.month)
Out[3]: <type 'int'>

In [4]: type(d.day)
Out[4]: <type 'int'>

Both are integers. So there is no automatic way to do what you want. So in the narrow sense, the answer to your question is no.

两者都是整数。所以没有自动的方法来做你想要的。所以从狭义上讲,你的问题的答案是否定的。

If you want leading zeroes, you'll have to format them one way or another. For that you have several options:

如果你想要前导零,你将不得不以这种或那种方式格式化它们。为此你有几个选择:

In [5]: '{:02d}'.format(d.month)
Out[5]: '03'

In [6]: '%02d' % d.month
Out[6]: '03'

In [7]: d.strftime('%m')
Out[7]: '03'

#2


10  

you can use a string formatter to pad any integer with zeros. It acts just like C's printf.

您可以使用字符串格式化程序用零填充任何整数。它就像C的printf一样。

>>> d = datetime.date.today()
>>> '%02d' % d.month
'03'

Updated for py36: Use f-strings! For general ints you can use the d formatter and explicitly tell it to pad with zeros:

针对py36更新:使用f-strings!对于一般的整数,您可以使用d格式化程序并明确告诉它用零填充:

 >>> d = datetime.date.today()
 >>> f"{d.month:02d}"
 '07'

But datetimes are special and come with special formatters that are already zero padded:

但是,日期时间是特殊的,并且带有已填零的特殊格式化程序:

 >>> f"{d:%d}"  # the day
 '01'
 >>> f"{d:%m}"  # the month
 '07'