Vii. Sana va vaqt bilan ishlash


Download 21.66 Kb.
bet2/6
Sana28.10.2023
Hajmi21.66 Kb.
#1732373
1   2   3   4   5   6
Bog'liq
python time

from datetime import time
current_time = time() print(current_time) # 00:00:00
current_time = time(16, 25) print(current_time) # 16:25:00
current_time = time(16, 25, 45) print(current_time) # 16:25:45

datetime sinfi


datetime sinfi bir vaqtning o‟zida sana va vaqt bilan ishlash imkoniyatini
yaratadi. datetime sinfi obyektini yaratish uchun quyidagi konstruktor ishlatiladi:

1

datetime(year, month, day [, hour] [, min] [, sec] [, microsec])

Birinchi uchta parametr yil, oy va kunlar zaruriy parametrlar hisoblanadi, qolgan uchtasi esa shart bo‟lmagan, hamda, kelishuv bo‘yicha ular nol qiymat oladi.

1
2 3
4
5
6
7

from datetime import datetime
deadline = datetime(2017, 5, 10) print(deadline) # 2017-05-10 00:00:00
deadline = datetime(2017, 5, 10, 4, 30) print(deadline) # 2017-05-10 04:30:00

Joriy vaqtni va sanani olish uchun now() metodidan foydalaniladi.

1
2
3
4
5
6
7 8
9
10

from datetime import datetime
now = datetime.now()
print(now) # 2017-05-03 11:18:56.239443
print("{}.{}.{} {}:{}".format(now.day, now.month, now.year, now.hour, now.minute)) # 3.5.2017 11:21
print(now.date()) print(now.time())

day, month, year, hour, minute, second parametrlari orqali sana va vaqtning alohida qiymatlarini olish mumkin. date() va time() metodlari orqali esa mos ravishda alohida sana va vaqtni olish mumkin.

Download 21.66 Kb.

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




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