2015-01-28 11 views

Antwort

11

Wie wäre es

today = datetime.today() 
datem = datetime(today.year, today.month, 1) 

Ich gehe davon aus Sie den 1. des Monats.

+0

Thx. Gute Variante. Ich habe alle Ansichten geändert und ein gutes Ergebnis erzielt. – user3778327

+1

@ user3778327: 'today'-Variable hat bereits das aktuelle Jahr und den aktuellen Monat. – jfs

+2

Für diese Lösung verwenden Sie bitte 'from datetime import datetime' anstatt einfach' import datetime' – jpobst

23

Versuchen Sie, diese Lösung:

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 
2

Sie können immer sub-string-Methode verwenden

import datetime; 

today = str(datetime.date.today()); 
curr_year = int(today[:4]); 
curr_month = int(today[5:7]); 

Dies wird Ihnen aktuellen Monat und Jahr in Integer-Format erhalten. Wenn Sie wollen, dass sie eine Zeichenkette sind, müssen Sie einfach die Priorität "int" entfernen, während Sie den Variablen curr_year und curr_month Werte zuweisen.

+1

Das ist schlecht. Warum müssen Sie es umwandeln und dann eine String-Manipulation durchführen? '' 'datetime.datetime.now() .monate''' ist besser. – Ahmed

1

Ich bin zu spät, aber jemand wird dies in der Zukunft nützlich finden.

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

Ref: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

Ref: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

Above Dinge sind nützlich für jedes Datum Parsen nicht nur jetzt oder heute, es kann für ein beliebiges Datum Parsing nützlich sein.

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') 

Und so weiter ....

Hoffnung jemand diesen Kommentar hilfreich.

Verwandte Themen