2009-07-15 13 views

risposta

64

E 'infatti quasi la stessa in Python .. :-)

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

Naturalmente, la data non ha un tempo associato, quindi se sei in auto e anche su questo, si può fare lo stesso con un oggetto datetime completo:

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

(Ovviamente non è diverso, ma è possibile memorizzare datetime.datetime.today() in una variabile prima di afferrare l'anno, naturalmente).

Una cosa fondamentale da notare è che i componenti di tempo possono differire tra pitoni a 32-bit e 64-bit in alcune versioni di Python (albero 2.5.x credo). Quindi troverai cose come hour/min/sec su alcune piattaforme a 64 bit, mentre ottieni ore/minuti/secondi su 32-bit.

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

o addirittura (come suggerito Lennart)

a = datetime.datetime.now().year 

o anche

a = datetime.date.today().year 
+1

Anche se ora() si sente più naturale su un datetime. datetime.date.today(). Anno, forse. :) –

+0

Io non ci avevo pensato, e credo che lo fa sentire più "preciso" rispetto al tempo, dove oggi() potrebbe sembrare implicare una precisione di giorni. Stranamente (almeno in 2.5.4) datetime.today() e datetime.now() fanno la stessa cosa –

+0

Non sono esattamente la stessa cosa, datetime.now() accetta un oggetto timezone, mentre datetime.today() chiama solo fromtimestamp (time.time()), e così oggi() sarà sempre in qualunque Python pensa il fuso orario locale è, mentre è possibile ottenere ora() per dirvi le cose un po 'più interessanti. –

11

Le altre risposte a questa domanda sembrano colpire il posto giusto. Ora, come lo descriveresti da solo senza overflow dello stack? Scopri IPython, una shell Python interattiva che ha scheda di completamento automatico.

> 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. 

premi sul tasto tab un paio di volte e ti verrà richiesto con i membri dell ' "adesso" oggetto:

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 

e vedrai che now.year è membro di l'oggetto "ora".

+2

Inoltre, questo: http://docs.python.org/library/datatypes.html Ma grazie, non lo sapevo su IPython –

+2

Complimenti per il problema * risoluzione * - che completa la soluzione reale. –

2

Se si desidera che l'anno da un (sconosciuto) datetime-oggetto:

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 
Problemi correlati