๐Ÿ“‚์›น ๊ฐœ๋ฐœ(Web)/๐ŸํŒŒ์ด์ฌ(Python)

datetime ๋ชจ๋“ˆ

๐Ÿ‘ฉ‍๐ŸŽ“์ธํ…”๋ฆฌ๊ฐ์ž๐Ÿฅ” 2023. 2. 1. 17:31

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())