1.1 Python datetime.date sinfi
Python-da biz sinfdan date ob'ektlarni yaratishimiz mumkin. dateSana obyekti sanani (yil, oy va kun) ifodalaydi.
3-misol: Sanani ifodalash uchun sana obyekti
import datetime
d = datetime.date(2022, 12, 25)
print(d)
Chiqish
25.12.2022
Faqat sanani import qilish sinfi
Biz faqat sanani chiqarish uchun datetime moduldan date sinfni import qilishimiz mumkin.Masalan,
from datetime import date
d = date(2022, 12, 25)
print(d)
Chiqish
25.12.2022
Bu yerda faqat datedatetime moduldan sinfni from datetime import date import qiladi.
4-misol: today() yordamida joriy sanani oling
Date nomli sinfdan foydalanib, today() joriy sanani o'z ichiga olgan ob'ektni yaratishimiz mumkin . Masalan,
from datetime import date
# today() to get current date
todays_date = date.today()
print("Today's date =", todays_date)
Chiqish
Today's date = 27.12.2022
5-misol: Bugungi yil, oy va kunni chop etish
datetime ob'ektdan yil, oy, kun, hafta kuni va hokazolarni date orqali osongina olishimiz mumkin. Masalan,
from datetime import date
# bugungi sananing ba’zi obyektlari
today = date.today()
print("Current year:", today.year)
print("Current month:", today.month)
print("Current day:", today.day)
Chiqish
Current year: 2022
Current month: 12
Current day: 27
1.2 Python datetime.time sinfi
Sinfdan yaratilgan vaqt obyekti timemahalliy vaqtni ifodalaydi.
6-misol: Vaqtni ifodalash uchun vaqt ob'ekti
from datetime import time
# time(hour = 0, minute = 0, second = 0)
a = time()
print(a)
# time(hour, minute and second)
b = time(11, 34, 56)
print(b)
# time(hour, minute and second)
c = time(hour = 11, minute = 34, second = 56)
print(c)
# time(hour, minute, second, microsecond)
d = time(11, 34, 56, 234566)
print(d)
|