python datetime tutorial with examples
Python DateTime-handledning:
I vår tidigare handledning diskuterade vi Python OOP-koncept i detalj.
Vi lärde oss mer om klass, objekt, konstruktör och oops tillsammans med de olika pelarna i oops som Inheritance, Overloading, Overriding, and Data diding som stöds av Python.
I denna handledning diskuteras ett ytterligare pythonkoncept som är Python DateTime. Läs igenom vår Series of Python Tutorials för bättre förståelse av Python-koncept i detalj.
skillnad mellan vänster koppling och vänster yttre koppling i sql
Vad du kommer att lära dig:
Titta på VIDEO Tutorial
En detaljerad titt på Python DateTime:
Python DateTime
I Python är datum, tid och DateTime inbyggda klasser som ger oss ett antal inbyggda funktioner för att hantera DateTime.
Dessa funktioner används för att få aktuellt datum, tid och dag.
Låt oss se några av exemplen för alla ovanstående.
Exempel 1:
from datetime import date def test_date(): today = date.today() print(“Today’s date is”, today) test_date()
Produktion:
Dagens datum är 29-09-2018
Produktion:
Exempel 2:
from datetime import date def test_date(): today = date.today() #To print individual date componets print(“Date components are:”, today.day, today.month, today.year) test_date()
Produktion:
Datumkomponenter är: 29 9 2018
Produktion:
Exempel 3:
fördelar och nackdelar med Linux vs Windows
from datetime import date def test_date(): today = date.today() #To print the weekday number(0=Monday , 6=Sunday) print(“Weekday number is:”, today.weekday()) test_date()
Produktion:
Veckodagens nummer är: 5
Produktion:
Exempel 4:
from datetime import datetime def test_date(): today = datetime.now() #Print the curren date and time print(“Current date and time is:”, today) test_date()
Produktion:
Nuvarande datum och tid är: 2018-09-29 21: 26: 09.578260
Produktion:
Exempel 5:
from datetime import datetime def test_date(): time = datetime.time(datetime.now()) #to retrieve the current time print(“Current time is:”, time) test_date()
Produktion:
Nuvarande tid är: 21: 28: 32.980759
Produktion:
Formatera datum och tid med strftime () -metoden
Exempel 6:
import datetime print(“Current date and time is:”, datetime.datetime.now()) print(“Current date and time using strftime method:”, datetime.datetime.now().strftime(“%y-%m-%d-%H-%M”) print(“Current year is:”, datetime.date.today().strftime(“%Y”)) print(“Month of year is:”, datetime.date.today().strftime(“%B”)) print(“Week number of the year is:”, datetime.date.today().strftime(“%W”)) print(“Weekday of the week is:”, datetime.date.today().strftime(“%w”)) print(“Day of the year is:”, datetime.date.today().strftime(“%j”)) print(“Day of the month is:”, datetime.date.today().strftime(“%d”)) print(“Day of the week is:”, datetime.date.today().strftime(“%A”))
Produktion :
Nuvarande datum och tid är: 2018-09-29 21: 32: 30.643372
Aktuellt datum och tid med strftime-metoden: 18-09-29-21-32
Det aktuella året är: 2018
Årets månad är: september
Årets veckonummer är: 39
Veckodag i veckan är: 6
Årets dag är: 272
Månadens dag är: 29
Veckodagen är: lördag
Produktion:
hur man hittar nätverkssäkerhetsnyckel för mobil hotspot
Slutsats
I denna handledning diskuterade vi några av de ytterligare ämnena för inbyggda strängfunktioner och andra begrepp som att öppna och radera en fil och inbyggda metoder som tillhandahålls av DateTime i detalj.
Vår kommande handledning kommer att förklara Python String-funktioner i detalj !!
PREV-handledning | NÄSTA självstudie
Rekommenderad läsning
- Python-strängfunktioner
- Python-handledning för nybörjare (praktisk Python-träning)
- Fördjupade förklaringar om förmörkelser för nybörjare
- Pythons huvudfunktionshandledning med praktiska exempel
- Python String Split Tutorial
- Python-funktioner
- Python-variabler
- Python Tuple-handledning med praktiska exempel