How to extract year from Python date and time object?

I would like to extract the year from the current date using Python.

In C #, it looks like this:

DateTime a = DateTime.Now() a.Year 

What is required in Python?

+44
python datetime
Jul 15 '09 at 18:37
source share
4 answers

Actually it is almost the same in Python .. :-)

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

Of course, the date has no time associated with this, so if you need it too, you can do the same with the full datetime object:

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

(Of course, there is nothing else, but you can store datetime.datetime.today () in a variable before capturing the year, of course).

It should be noted that the time components may differ between 32-bit and 64-bit pythons in some versions of python (I think this is a 2.5.x tree). Thus, you will find things like hour / min / sec on some 64-bit platforms, while you get hour / minute / second on 32-bit ones.

+61
Jul 15 '09 at 18:44
source share
 import datetime a = datetime.datetime.today().year 

or even (as Lennart suggested)

 a = datetime.datetime.now().year 

or even

 a = datetime.date.today().year 
+13
Jul 15 '09 at 18:41
source share

Other answers to this question seem to have come up with it. Now, how would you understand that without? Check out IPython , an interactive Python shell that has auto-complete tabs.

 > 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 features. %quickref -> Quick reference. help -> Python 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. 

click the tab a few times and you will be offered the members of the "now" object:

 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 will see that now.year is a member of the now object.

+11
Jul 15 '09 at 18:52
source share

If you need a year from an unknown 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 
+2
04 Oct '13 at 12:26
source share



All Articles