9-misol: Yil, oy, soat, daqiqa va sekundni chop etish
from datetime import datetime
a = datetime(2022, 12, 28, 23, 55, 59, 342380)
print("Year =", a.year)
print("Month =", a.month)
print("Day =", a.day)
print("Hour =", a.hour)
print("Minute =", a.minute)
print("Timestamp =", a.timestamp())
Chiqish
Year = 2022
Month = 12
Day = 28
Hour = 23
Minute = 55
Timestamp = 1511913359.34238
2.Python datetime.strftime()
date , time yoki datetime obyektini strftime() yordamida sana va vaqtni ifodalovchi qatorni qaytaradi .
Quyidagi dastur datetime joriy sana va vaqtni o'z ichiga olgan ob'ektini turli qator formatlariga o'zgartiradi.
from datetime import datetime
now = datetime.now() # hozirgi sana va vaqt
year = now.strftime("%Y")
print("year:", year)
month = now.strftime("%m")
print("month:", month)
day = now.strftime("%d")
print("day:", day)
time = now.strftime("%H:%M:%S")
print("time:", time)
date_time = now.strftime("%d/%m/%Y, %H:%M:%S")
print("date and time:",date_time)
Dasturni ishga tushirganimizda, natija quyidagicha bo'ladi:
year: 2023
month: 01
day: 03
time: 14:30:31
date_time: 03.01.2023, 14:30:31
Bu yerda yil, kun, vaqt va sana_vaqt satrlaridir, bu ham datetime ob'ekti hisoblanadi.
Do'stlaringiz bilan baham: |