In [1]:
import datetime
In [2]:
dt_now = datetime.datetime.now()
print(dt_now)
In [3]:
print(type(dt_now))
In [4]:
print(dt_now.year)
In [5]:
print(dt_now.hour)
In [6]:
dt = datetime.datetime(2018, 2, 1, 12, 15, 30, 2000)
print(dt)
In [7]:
print(dt.minute)
In [8]:
print(dt.microsecond)
In [9]:
dt = datetime.datetime(2018, 2, 1)
print(dt)
In [10]:
print(dt.minute)
In [11]:
print(dt_now)
print(type(dt_now))
In [12]:
print(dt_now.date())
print(type(dt_now.date()))
In [13]:
d_today = datetime.date.today()
print(d_today)
In [14]:
print(type(d_today))
In [15]:
print(d_today.year)
In [16]:
d = datetime.date(2018, 2, 1)
print(d)
In [17]:
print(d.month)
In [18]:
t = datetime.time(12, 15, 30, 2000)
print(t)
In [19]:
print(type(t))
In [20]:
print(t.hour)
In [21]:
t = datetime.time()
print(t)
In [22]:
td = dt_now - dt
print(td)
In [23]:
print(type(td))
In [24]:
print(td.days)
In [25]:
print(td.seconds)
In [26]:
print(td.microseconds)
In [27]:
print(td.total_seconds())
In [28]:
td_1w = datetime.timedelta(weeks=1)
print(td_1w)
In [29]:
print(td_1w.days)
In [30]:
d_1w = d_today - td_1w
print(d_1w)
In [31]:
td_10d = datetime.timedelta(days=10)
print(td_10d)
In [32]:
dt_10d = dt_now + td_10d
print(dt_10d)
In [33]:
td_50m = datetime.timedelta(minutes=50)
print(td_50m)
In [34]:
print(td_50m.seconds)
In [35]:
dt_50m = dt_now + td_50m
print(dt_50m)
In [36]:
d_target = datetime.date(2020, 7, 24)
td = d_target - d_today
print(td)
In [37]:
print(td.days)
In [38]:
print(dt_now.strftime('%Y-%m-%d %H:%M:%S'))
In [39]:
print(d_today.strftime('%y%m%d'))
In [40]:
print(d_today.strftime('%A, %B %d, %Y'))
In [41]:
print(d_today.strftime('%Y年%m月%d日'))
In [42]:
print('日番号(1年の何日目か / 正月が001):', d_today.strftime('%j'))
print('週番号(週の始まりは日曜日 / 正月が00):', d_today.strftime('%U'))
print('週番号(週の始まりは月曜日 / 正月が00):', d_today.strftime('%W'))
In [43]:
week_num_mon = int(d_today.strftime('%W'))
print(week_num_mon)
print(type(week_num_mon))
In [44]:
d = datetime.date(2018, 2, 1)
td = datetime.timedelta(weeks=2)
n = 8
f = '%Y年%m月%d日'
l = []
for i in range(n):
l.append((d + i * td).strftime(f))
In [45]:
print(l)
In [46]:
print('\n'.join(l))
In [47]:
l = [(d + i * td).strftime(f) for i in range(n)]
print(l)
In [48]:
date_str = '2018/2/1 12:30'
date_dt = datetime.datetime.strptime(date_str, '%Y/%m/%d %H:%M')
print(date_dt)
In [49]:
print(type(date_dt))
In [50]:
print(date_dt.strftime('%Y年%m月%d日 %H時%M分'))
In [51]:
date_str = '2018年2月1日'
date_format = '%Y年%m月%d日'
td_10_d = datetime.timedelta(days=10)
date_dt = datetime.datetime.strptime(date_str, date_format)
date_dt_new = date_dt - td_10_d
date_str_new = date_dt_new.strftime(date_format)
print(date_str_new)