날짜/시간 관련 패키지
In [1]:
import datetime
In [2]:
dt = datetime.datetime.now()
dt
Out[2]:
In [3]:
type(dt)
Out[3]:
year
, month
, day
, hour
, minute
, second
, microsecond
tzinfo
weekday()
: 요일 {0:월, 1:화, 2:수, 3:목, 4:금, 5:토, 6:일}strftime(format)
: datetime -> 문자열toordinal()
: datetime -> proleptic Gregorian ordinal (엑셀 날짜)date()
: datetime -> datetime()
: datetime -> time
In [4]:
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo
Out[4]:
In [5]:
dt.weekday() # {0:월, 1:화, 2:수, 3:목, 4:금, 5:토, 6:일}
Out[5]:
In [6]:
dt1 = datetime.datetime.strptime("2015-12-31 11:32", "%Y-%m-%d %H:%M")
dt1
Out[6]:
In [7]:
dt1.strftime("%d/%m/%y")
Out[7]:
In [8]:
dt1.strftime("%A %d. %B %Y")
Out[8]:
%Y
: 년, zero-padded decimal%m
: 월, zero-padded decimal%d
: 일, zero-padded decimal%H
: 시, zero-padded decimal%M
: 분, zero-padded decimal%S
: 초, zero-padded decimal
In [9]:
s = dt1.strftime(u"%Y년 %m월 %d일 %H시 %M분 %S초".encode("utf-8"))
print(s)
In [10]:
dt = datetime.datetime.now()
dt
Out[10]:
In [11]:
dt.date(), dt.time()
Out[11]:
In [12]:
d = datetime.date(2015, 12, 31)
d
Out[12]:
In [13]:
t = datetime.time(11, 31, 29)
t
Out[13]:
In [14]:
datetime.datetime.combine(d, t)
Out[14]:
In [15]:
dt1 = datetime.datetime(2016, 2, 19, 14)
dt2 = datetime.datetime(2016, 1, 2, 13)
td = dt1 - dt2
td
Out[15]:
In [16]:
td.days, td.seconds, td.microseconds
Out[16]:
In [17]:
td.total_seconds()
Out[17]:
In [18]:
import time
print("start...")
time.sleep(1)
print(1)
time.sleep(1)
print(2)
time.sleep(1)
print(3)
time.sleep(1)
print(4)
time.sleep(1)
print("finish!")
In [19]:
time.time()
Out[19]:
In [20]:
ts = time.localtime()
ts
Out[20]:
In [21]:
time.mktime(ts)
Out[21]:
In [22]:
import pytz
In [23]:
seoul = pytz.timezone("Asia/Seoul")
In [24]:
t1 = datetime.datetime.now()
t1
Out[24]:
In [25]:
lt1 = seoul.localize(t1)
lt1
Out[25]:
In [26]:
t2 = datetime.datetime.utcnow()
t2
Out[26]:
In [27]:
lt2 = pytz.utc.localize(t2)
lt2
Out[27]:
In [28]:
lt2 = t2.replace(tzinfo=pytz.utc)
lt2
Out[28]:
In [29]:
t1 = datetime.datetime.now()
lt1 = seoul.localize(t1)
lt3 = lt1.astimezone(pytz.timezone("US/Eastern"))
lt3
Out[29]:
In [1]:
from dateutil.parser import parse
In [3]:
parse('2016-04-16')
Out[3]:
In [10]:
parse("Apr 16, 2016 04:05:32 PM")
Out[10]:
In [12]:
parse('6/7/2016')
Out[12]: