datetime
๋ ์ง์ ์๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ๋ ์ฌ์ฉํ๋ค
1. import datetime
# ํ์ฌ ๋ ์ง์ ์๊ฐ ๋ณํ, ๋ง์ดํฌ๋ก ์ด ๋จ์ ์ถ๋ ฅ
print(datetime.datetime.now())
print(datetime.datetime.today())
2. date() ํจ์ ํน์ ๋ ์ง๋ฅผ ๋ง๋ค์ด ๋ฐํ
print(datetime.date(2023, 1, 30))
print(datetime.time(10, 40, 0))
3. ๋ ์ง ํ๋๊ฐ
y = datetime.datetime.now().year
m = datetime.datetime.now().month
d = datetime.datetime.now().day
h = datetime.datetime.now().hour
mi = datetime.datetime.now().minute
s = datetime.datetime.now().second
print('{}๋
{}์ {}์ผ {}:{}:{}'.format(y,m,d,h,mi,s))
4. timedelta ๋ ์ง/์๊ฐ ๋ฐ์ดํฐ ์ฐ์ฐ
#์ด์
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
print(yesterday)
#๋ด์ผ
tomorrow = today + datetime.timedelta(days=1)
print(tomorrow)
#๋ ์ง ์ฐจ์ด
date1 = datetime.date(2023,1,30)
date2 = datetime.date(2022,12,24)
print(date1 - date2)
#์ด๋ก ๋ณํ
print((date1-date2).total_seconds())
'๐์น ๊ฐ๋ฐ(Web) > ๐ํ์ด์ฌ(Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ผ ๋ง๋ค๊ณ ์ฝ๊ธฐ (0) | 2023.02.06 |
---|---|
์ง์ญ๋ณ์(local) ์ ์ญ๋ณ์(global) (0) | 2023.02.01 |
ํ์ด์ฌ time ๋ชจ๋ (0) | 2023.02.01 |
ํ์ด์ฌ ๋๋ค random ๋ชจ๋ (0) | 2023.02.01 |
ํ์ด์ฌ ํ์ค ๋ชจ๋ (0) | 2023.02.01 |