Devo avere l'anno e il mese correnti in datetime.
Io uso questo:
datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")
C'è forse un altro modo?
Devo avere l'anno e il mese correnti in datetime.
Io uso questo:
datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")
C'è forse un altro modo?
Risposte:
Uso:
from datetime import datetime
today = datetime.today()
datem = datetime(today.year, today.month, 1)
Presumo tu voglia il primo del mese.
todayvariabile ha già l'anno e il mese correnti.
from datetime import datetimepiuttosto che semplicementeimport datetime
Prova questa soluzione:
from datetime import datetime
currentSecond= datetime.now().second
currentMinute = datetime.now().minute
currentHour = datetime.now().hour
currentDay = datetime.now().day
currentMonth = datetime.now().month
currentYear = datetime.now().year
.now()a .today()quello usato in altre risposte? La now()funzione è più breve, è più comunemente usata ... ma c'è qualche motivo per usarne una sull'altra quando tutto quello che si vuole sapere è che anno è?
from datetimee perché ne hai bisogno?
datesottomodulo ( from datetime import date) che si occupa solo delle date, il timesottomodulo che si occupa dei tempi e il datetimesottomodulo sfortunatamente chiamato che fa entrambe le cose. C'è anche timedeltae tzinfolì dentro. Si potrebbe semplicemente import datetimeottenere l'intero pacchetto con tutti i sottomoduli, ma le chiamate ai metodi sarebbero simili a datetime.datetime.now()o datetime.date.today(). In genere, è meglio per diversi motivi importare solo i componenti di cui hai bisogno.
Uso:
from datetime import datetime
current_month = datetime.now().strftime('%m') // 02 //This is 0 padded
current_month_text = datetime.now().strftime('%h') // Feb
current_month_text = datetime.now().strftime('%B') // February
current_day = datetime.now().strftime('%d') // 23 //This is also padded
current_day_text = datetime.now().strftime('%a') // Fri
current_day_full_text = datetime.now().strftime('%A') // Friday
current_weekday_day_of_today = datetime.now().strftime('%w') //5 Where 0 is Sunday and 6 is Saturday.
current_year_full = datetime.now().strftime('%Y') // 2018
current_year_short = datetime.now().strftime('%y') // 18 without century
current_second= datetime.now().strftime('%S') //53
current_minute = datetime.now().strftime('%M') //38
current_hour = datetime.now().strftime('%H') //16 like 4pm
current_hour = datetime.now().strftime('%I') // 04 pm
current_hour_am_pm = datetime.now().strftime('%p') // 4 pm
current_microseconds = datetime.now().strftime('%f') // 623596 Rarely we need.
current_timzone = datetime.now().strftime('%Z') // UTC, EST, CST etc. (empty string if the object is naive).
Riferimento: 8.1.7. Comportamento strftime () e strptime ()
Riferimento: strftime () e strptime () Behavior
Le cose di cui sopra sono utili per qualsiasi analisi della data, non solo ora o oggi. Può essere utile per qualsiasi analisi della data.
e.g.
my_date = "23-02-2018 00:00:00"
datetime.strptime(str(my_date),'%d-%m-%Y %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S+00:00')
datetime.strptime(str(my_date),'%d-%m-%Y %H:%M:%S').strftime('%m')
E così via...
Puoi scrivere la risposta accettata come una riga usando date.replace:
datem = datetime.today().replace(day=1)
Puoi sempre utilizzare un metodo di sottostringa:
import datetime;
today = str(datetime.date.today());
curr_year = int(today[:4]);
curr_month = int(today[5:7]);
In questo modo otterrai il mese e l'anno correnti in formato intero. Se vuoi che siano stringhe devi semplicemente rimuovere la precedenza "int" mentre assegni i valori alle variabili curr_yeare curr_month.
datetime.datetime.now().monthè meglio.
Risposta tardiva, ma puoi anche usare:
import time
ym = time.strftime("%Y-%m")
La domanda chiede di utilizzare datetime in modo specifico.
Questo è un modo che utilizza solo datetime:
year = datetime.now().year
month = datetime.now().month
date.today().strftime("%Y-%m")?