python datetime tutorial with examples
Tutorial de Python DateTime:
En el nostre tutorial anterior, vam parlar Concepte OOP de Python en detall.
Vam aprendre més coses sobre la classe, els objectes, el constructor i els Oops, juntament amb els diferents pilars de Oops, com ara Herència, Sobrecàrrega, Anul·lació i Ocultació de dades que Python admet.
En aquest tutorial es parlarà d'un concepte addicional de python que és Python DateTime. Llegiu el nostre document Sèrie de tutorials de Python per a una millor comprensió detallada dels conceptes de Python.
el millor convertidor de fitxers gratuït per a Windows 10
Què aprendreu:
Mireu el tutorial VIDEO
Un cop d'ull detallat a Python DateTime:
Python DateTime
A Python, data, hora i DateTime són classes incorporades que ens proporcionen una sèrie de funcions incorporades per fer front a DateTime.
Aquestes funcions s’utilitzen per obtenir la data, l’hora i el dia actuals.
Vegem alguns exemples de tot l’anterior.
Exemple 1:
from datetime import date def test_date(): today = date.today() print(“Today’s date is”, today) test_date()
Sortida:
La data d’avui és el 29-09-2018
Sortida:
Exemple 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()
Sortida:
Els components de la data són: 29 9 2018
Sortida:
Exemple 3:
com obriu un fitxer swf
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()
Sortida:
El número del dia feiner és: 5
Sortida:
Exemple 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()
Sortida:
La data i l’hora actual són: 29-09-2018 a les 21: 26: 09.578260
Sortida:
Exemple 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()
Sortida:
el millor convertidor gratuït de DVD a AVI
L’hora actual és: 21: 28: 32.980759
Sortida:
Format de data i hora mitjançant el mètode strftime ()
Exemple 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”))
Sortida :
Data i hora actuals són: 29-09-2018 21: 32: 30.643372
Data i hora actual mitjançant el mètode strftime: 18-09-29-21-32
L’any en curs és: 2018
El mes de l'any és: setembre
El número setmanal de l'any és: 39
El dia de la setmana és: 6
El dia de l'any és: 272
El dia del mes és: 29
El dia de la setmana és: dissabte
Sortida:
Conclusió
En aquest tutorial hem comentat alguns dels temes addicionals de les funcions integrades de cadenes i altres conceptes com obrir i suprimir un fitxer i els mètodes incorporats proporcionats per DateTime en detall.
El nostre proper tutorial explicarà detalladament les funcions de Python String.
Lectura recomanada
- Funcions de cadena de Python
- Tutorial Python per a principiants (entrenament pràctic GRATU PyT en Python)
- Tutorials Eclipse en profunditat per a principiants
- Tutorial de funcions principals de Python amb exemples pràctics
- Tutorial de Python String Split
- Funcions Python
- Variables de Python
- Tutorial Python Tuple amb exemples pràctics