Python dasturlash tilida sana va vaqt bilan ishlash modullari


-misol: Yil, oy, soat, daqiqa va sekundni chop etish


Download 48.9 Kb.
bet5/7
Sana30.04.2023
Hajmi48.9 Kb.
#1406358
1   2   3   4   5   6   7
Bog'liq
marufovmuhammad algoritmik tillar (1)

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 .

1-misol: strftime() yordamida qatorga datetime


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.


Download 48.9 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling