Risposte:
Puoi usare il datetimemodulo per lavorare con date e ore in Python. Il strftimemetodo consente di produrre una rappresentazione di stringa di date e ore con un formato specificato.
>>> import datetime
>>> datetime.date.today().strftime("%B %d, %Y")
'July 23, 2010'
>>> datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
'10:36AM on July 23, 2010'
#python3
import datetime
print(
'1: test-{date:%Y-%m-%d_%H:%M:%S}.txt'.format( date=datetime.datetime.now() )
)
d = datetime.datetime.now()
print( "2a: {:%B %d, %Y}".format(d))
# see the f" to tell python this is a f string, no .format
print(f"2b: {d:%B %d, %Y}")
print(f"3: Today is {datetime.datetime.now():%Y-%m-%d} yay")
1: test-2018-02-14_16: 40: 52.txt
2a: 4 marzo 2018
2b: 4 marzo 2018
3: Oggi è il 2018-11-11 yay
Descrizione:
Utilizzando il nuovo formato di stringa per inserire un valore in una stringa nel segnaposto {}, il valore è l'ora corrente.
Quindi, invece di visualizzare semplicemente il valore non elaborato come {}, utilizza la formattazione per ottenere il formato della data corretto.
https://docs.python.org/3/library/string.html#formatexamples
fsignifica in print(f"3?
%Be cosa rappresentano.
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%B %d, %Y")
'July 23, 2010'