๐Ÿ“‚ Programming/Python

[Python] datetime ๋ชจ๋“ˆ ์ •๋ฆฌ

dhyuck 2023. 4. 21. 01:29
๋ฐ˜์‘ํ˜•
import datetime

now = datetime.datetime.now()
one_day = datetime.timedelta(days=1)
one_hour = datetime.timedelta(hours=1)

# ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ์ถœ๋ ฅ
# 2023-04-21 01:21:35.277992
print(f'now : {now}') 

# ํ˜„์žฌ ์—ฐ๋„์™€ ๋‚ ์งœ๋งŒ ์ถœ๋ ฅ
# 2023-04-21
print(f'now.date() : {now.date()}') 

# ํ˜„์žฌ ์‹œ๊ฐ„๋งŒ ์ถœ๋ ฅ
# 01:21:35.277992
print(f'now.time() : {now.time()}') 

# ํ˜„์žฌ ์—ฐ๋„๋งŒ ์ถœ๋ ฅ
# 2023
print(f'now.year : {now.year}')

# ํ˜„์žฌ ์›”๋งŒ ์ถœ๋ ฅ
# 4
print(f'now.month : {now.month}')

# ํ˜„์žฌ ์ผ๋งŒ ์ถœ๋ ฅ
# 21
print(f'now.day : {now.day}')

# ํ˜„์žฌ ์š”์ผ ์ถœ๋ ฅ(0:์›”์š”์ผ ~ 6:์ผ์š”์ผ)
# 4
print(f'now.weekday : {now.weekday()}') 

# ๋‚ ์งœ๋ฅผ ํŠน์ • ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ
# 23/04/21-01:21:35
print(f'now.strftime("%y/%m/%d-%H:%M:%S") : {now.strftime("%y/%m/%d-%H:%M:%S")}')

# ๋‚ด์ผ์„ ๊ณ„์‚ฐํ•˜๋Š”ํ•˜์—ฌ ์ถœ๋ ฅ
# 2023-04-22 01:21:35.277992
print(f'1 day later : {now + one_day}')

# 1์‹œ๊ฐ„ ํ›„๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ถœ๋ ฅ
# 2023-04-21 02:21:35.277992
print(f'1 hour later : {now + one_hour}')
๋ฐ˜์‘ํ˜•

'๐Ÿ“‚ Programming > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Python] collections Counter ์‚ฌ์šฉ๋ฒ•  (0) 2021.09.06
[Python] ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ  (0) 2021.08.21