๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT-Engineering/Python

ํŒŒ์ด์ฌ ๋‚ ์งœ-์‹œ๊ฐ„ ์‚ฌ์šฉ๊ณผ ๊ณ„์‚ฐ์— ์œ ์šฉํ•œ datetime ๋ชจ๋“ˆ ํ™œ์šฉ ์˜ˆ์‹œ ๋ชจ์Œ

by ๐Ÿงž‍โ™‚๏ธ 2020. 9. 22.
๋ฐ˜์‘ํ˜•

 

ํŒŒ์ด์ฌ์—์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๋“  ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋“  pandas์™€ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋“  ๋‚ ์งœ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ผ์ด ์ œ๋ฒ• ์žˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ”ํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” datetime ๋ชจ๋“ˆ์˜ ์‚ฌ์šฉ ์˜ˆ์‹œ๋“ค์„ ๋…ธํŠธ๋ถ์—์„œ ์ž‘์—…ํ•œ ๋‚ด์—ญ์„ ์ญ‰ ๋ถ™์—ฌ์„œ ๋ณด์—ฌ์คŒ์œผ๋กœ์จ datetime์˜ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ํ™œ์šฉ์„ ๋•๊ณ ์ž ํ•œ๋‹ค.

datetime๋งค๋‰ด์–ผ์„ ํ†ตํ•ด ๋”์šฑ ์ž์„ธํžˆ ํ™•์ธํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ํญ๋„“๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ฒ˜์Œ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์—์„œ๋Š” ์•„๋ž˜์—์„œ ๋ณด์ธ ์˜ˆ์‹œ๋“ค๋งŒ์œผ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฆฌ๋ผ ๋ณธ๋‹ค.

 

datetime ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๊ธฐ

In [2]:
import datetime as dt
 

now()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ์‹œ๊ฐ„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

In [3]:
current_time = dt.datetime.now()
In [4]:
current_time
Out[4]:
datetime.datetime(2020, 9, 22, 18, 4, 52, 612455)
 

datetime.datetime๊ฐ์ฒด์˜ strftimeํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋‚ ์งœ-์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ

In [5]:
dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Out[5]:
'2020-09-22 18:04:52'
 

datetime๊ฐ์ฒด ์ž„์˜๋กœ ์ƒ์„ฑํ•˜๊ธฐ

In [6]:
old_time = dt.datetime(2018,9,11,0,0,0)
In [7]:
old_time
Out[7]:
datetime.datetime(2018, 9, 11, 0, 0)
 

datetime๊ฐ์ฒด๋ผ๋ฆฌ์˜ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋‚ ์งœ-์‹œ๊ฐ„ ์ฐจ์ด ๊ณ„์‚ฐํ•˜๊ธฐ (timedelta ๊ฐ์ฒด)

In [8]:
delta_time = current_time - old_time
In [9]:
delta_time
Out[9]:
datetime.timedelta(days=742, seconds=65092, microseconds=612455)
 

๋‚ ์งœ-์‹œ๊ฐ„์œผ๋กœ๋ถ€ํ„ฐ ํƒ€์ž„์Šคํƒฌํ”„ ๊ฐ’ ์–ป์–ด์˜ค๊ธฐ

In [10]:
current_time.timestamp()
Out[10]:
1600765492.612455
 

ํƒ€์ž„์Šคํƒฌํ”„๋กœ๋ถ€ํ„ฐ ๋‚ ์งœ-์‹œ๊ฐ„ ์–ป์–ด์˜ค๊ธฐ

In [11]:
dt.datetime.fromtimestamp(current_time.timestamp())
Out[11]:
datetime.datetime(2020, 9, 22, 18, 4, 52, 612455)
 

๋ฌธ์ž์—ด๋กœ๋ถ€ํ„ฐ ์‹œ๊ฐ„ ์–ป์–ด์˜ค๊ธฐ

In [12]:
datestr = '20200921' # ์ž„์˜์˜ ๋‚ ์งœ ๋ฌธ์ž์—ด
In [13]:
dt.datetime.strptime(datestr,"%Y%m%d") # ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์ž๋ฆฌ์ˆ˜ ๋“ฑ ํฌ๋งท์— ๋งž๊ฒŒ ๋’ค์— ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•จ
Out[13]:
datetime.datetime(2020, 9, 21, 0, 0)
 

์œ„์˜ ๋‚ ์งœ-์‹œ๊ฐ„ ๊ฐ์ฒด๋ฅผ ๋‚ ์งœ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

In [14]:
dt.datetime.strptime(datestr,"%Y%m%d").date() # datetime๊ฐ์ฒด์—์„œ ๋‚ ์งœ ๊ฐ์ฒด date๋กœ ๋ณ€ํ™˜
Out[14]:
datetime.date(2020, 9, 21)
 

๋‚ ์งœ-์‹œ๊ฐ„์—์„œ ๋‚ ์งœ๋‚˜ ์‹œ๊ฐ„ ๋”ํ•˜๊ธฐ/๋นผ๊ธฐ

In [15]:
next_time = current_time + dt.timedelta(seconds=30) # 30์ดˆ ๋”ํ•˜๊ธฐ
print(next_time)
 
2020-09-22 18:05:22.612455
In [16]:
next_time = current_time + dt.timedelta(hours=1, minutes=10) # 1์‹œ๊ฐ„ 10๋ถ„ ๋”ํ•˜๊ธฐ
print(next_time)
 
2020-09-22 19:14:52.612455
In [17]:
next_time = current_time + dt.timedelta(days=7) # 7์ผ ๋”ํ•˜๊ธฐ
print(next_time)
 
2020-09-29 18:04:52.612455
 

datetime ๊ฐ์ฒด์—์„œ ์—ฐ, ์›”, ์ผ ๋“ฑ ์–ป์–ด์˜ค๊ธฐ

In [18]:
current_time.year # ์—ฐ๋„
Out[18]:
2020
In [19]:
current_time.month # ์›”
Out[19]:
9
In [20]:
current_time.day # ์ผ
Out[20]:
22
In [21]:
current_time.hour # ์‹œ๊ฐ„
Out[21]:
18
In [22]:
current_time.minute # ๋ถ„
Out[22]:
4
In [23]:
current_time.second # ์ดˆ
Out[23]:
52
 

๋‚ ์งœ ๊ฐ์ฒด๋ผ๋ฆฌ ์—ฐ์‚ฐํ•˜๊ธฐ

In [24]:
date1 = dt.datetime(2020,9,22).date()
date2 = dt.datetime(2020,9,3).date()
In [25]:
date1 - date2
Out[25]:
datetime.timedelta(days=19)
 

์š”์ผ ์–ป์–ด์˜ค๊ธฐ (์›”:0, ํ™”:1, ์ˆ˜:2, ๋ชฉ:3, ๊ธˆ:4, ํ† :5, ์ผ:6)

In [26]:
current_time.weekday()
Out[26]:
1
In [27]:
date2.weekday()
Out[27]:
3
 
 

๋…ธํŠธ๋ถ ๋งํฌ: github.com/skyiwalker/gentlesark/blob/master/notebooks/datetime-study.ipynb

 

skyiwalker/gentlesark

Contribute to skyiwalker/gentlesark development by creating an account on GitHub.

github.com

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€