简体   繁体   English

如何从 Python 日期时间 object 中提取年份?

[英]How to extract the year from a Python datetime object?

I would like to extract the year from the current date using Python.我想使用 Python 从当前日期提取年份。

In C#, this looks like:在 C# 中,这看起来像:

 DateTime a = DateTime.Now() 
 a.Year

What is required in Python? Python需要什么?

It's in fact almost the same in Python.. :-) 事实上它在Python中几乎相同.. :-)

import datetime
year = datetime.date.today().year

Of course, date doesn't have a time associated, so if you care about that too, you can do the same with a complete datetime object: 当然,date没有时间关联,所以如果你也关心它,你可以对完整的datetime对象做同样的事情:

import datetime
year = datetime.datetime.today().year

(Obviously no different, but you can store datetime.datetime.today() in a variable before you grab the year, of course). (显然没有什么不同,但你可以在获取年份之前将datetime.datetime.today()存储在变量中)。

One key thing to note is that the time components can differ between 32-bit and 64-bit pythons in some python versions (2.5.x tree I think). 需要注意的一件事是,在某些python版本中,时间组件在32位和64位pyth之间可能不同(我认为是2.5.x树)。 So you will find things like hour/min/sec on some 64-bit platforms, while you get hour/minute/second on 32-bit. 所以你会在某些64位平台上找到像小时/分钟/秒这样的东西,而在32位上你会得到小时/分钟/秒。

import datetime
a = datetime.datetime.today().year

or even (as Lennart suggested ) 甚至(正如Lennart建议的那样

a = datetime.datetime.now().year

or even 甚至

a = datetime.date.today().year

The other answers to this question seem to hit it spot on. 这个问题的其他答案似乎点击了它。 Now how would you figure this out for yourself without stack overflow? 现在如何在没有堆栈溢出的情况下为自己解决这个问题? Check out IPython , an interactive Python shell that has tab auto-complete. 查看IPython ,这是一个具有选项卡自动完成功能的交互式Python shell。

> ipython
import Python 2.5 (r25:51908, Nov  6 2007, 16:54:01)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.2.svn.r2750 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import datetime
In [2]: now=datetime.datetime.now()
In [3]: now.

press tab a few times and you'll be prompted with the members of the "now" object: 按几次选项卡,系统会提示您显示“now”对象的成员:

now.__add__           now.__gt__            now.__radd__          now.__sub__           now.fromordinal       now.microsecond       now.second            now.toordinal         now.weekday
now.__class__         now.__hash__          now.__reduce__        now.astimezone        now.fromtimestamp     now.min               now.strftime          now.tzinfo            now.year
now.__delattr__       now.__init__          now.__reduce_ex__     now.combine           now.hour              now.minute            now.strptime          now.tzname
now.__doc__           now.__le__            now.__repr__          now.ctime             now.isocalendar       now.month             now.time              now.utcfromtimestamp
now.__eq__            now.__lt__            now.__rsub__          now.date              now.isoformat         now.now               now.timetuple         now.utcnow
now.__ge__            now.__ne__            now.__setattr__       now.day               now.isoweekday        now.replace           now.timetz            now.utcoffset
now.__getattribute__  now.__new__           now.__str__           now.dst               now.max               now.resolution        now.today             now.utctimetuple

and you'll see that now.year is a member of the "now" object. 你会看到now.year是“now”对象的成员。

If you want the year from a (unknown) datetime-object: 如果您想要来自(未知)datetime-object的年份:

tijd = datetime.datetime(9999, 12, 31, 23, 59, 59)

>>> tijd.timetuple()
time.struct_time(tm_year=9999, tm_mon=12, tm_mday=31, tm_hour=23, tm_min=59, tm_sec=59, tm_wday=4, tm_yday=365, tm_isdst=-1)
>>> tijd.timetuple().tm_year
9999

It's easy to extract the year as follows.提取年份很容易,如下所示。

from datetime import datetime

year = datetime.today().year

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM