如何从Python日期时间对象中提取年份? [英] How to extract the year from a Python datetime object?
问题描述
我想使用Python从当前日期中提取年份。
I would like to extract the year from the current date using Python.
在C#中,这看起来像:
In C#, this looks like:
DateTime a = DateTime.Now()
a.Year
Python需要什么?
What is required in Python?
推荐答案
实际上在Python中几乎是相同的..:- )
It's in fact almost the same in Python.. :-)
import datetime
year = datetime.date.today().year
当然,日期没有时间,因此,如果您也关心日期,则可以使用完整的datetime进行操作对象:
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:
import datetime
year = datetime.datetime.today().year
(显然没有什么不同,但是您当然可以在获取年份之前将datetime.datetime.today()存储在变量中)
(Obviously no different, but you can store datetime.datetime.today() in a variable before you grab the year, of course).
要注意的一件事是在某些python版本(我认为是2.5.x树)中,时间分量在32位和64位python之间可能有所不同。因此,在某些64位平台上,您会发现诸如时/分/秒之类的信息,而在32位平台上,您会发现时/分/秒。
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). So you will find things like hour/min/sec on some 64-bit platforms, while you get hour/minute/second on 32-bit.
这篇关于如何从Python日期时间对象中提取年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!