In [1]:
import datetime

In [2]:
s = '2018-12-31'

In [3]:
d = datetime.date.fromisoformat(s)

In [4]:
print(d)


2018-12-31

In [5]:
print(type(d))


<class 'datetime.date'>

In [6]:
# print(datetime.date.fromisoformat('2018-12'))
# ValueError: Invalid isoformat string: '2018-12'

In [7]:
print(datetime.date.fromisoformat('2018-01-01'))


2018-01-01

In [8]:
# print(datetime.date.fromisoformat('2018-1-1'))
# ValueError: Invalid isoformat string: '2018-1-1'

In [9]:
s = '05:00:30.001000'

In [10]:
t = datetime.time.fromisoformat(s)

In [11]:
print(t)


05:00:30.001000

In [12]:
print(type(t))


<class 'datetime.time'>

In [13]:
print(datetime.time.fromisoformat('05'))


05:00:00

In [14]:
# print(datetime.time.fromisoformat('5:00:30'))
# ValueError: Invalid isoformat string: '5:00:30'

In [15]:
s = '2018-12-31T05:00:30.001000'

In [16]:
dt = datetime.datetime.fromisoformat(s)

In [17]:
print(dt)


2018-12-31 05:00:30.001000

In [18]:
print(type(dt))


<class 'datetime.datetime'>

In [19]:
print(datetime.datetime.fromisoformat('2018-12-31x05:00:30.001000'))


2018-12-31 05:00:30.001000

In [20]:
# print(datetime.datetime.fromisoformat('2018-12-31xx05:00:30.001000'))
# ValueError: Invalid isoformat string: '2018-12-31xx05:00:30.001000'

In [21]:
print(datetime.datetime.fromisoformat('2018-12-31T05'))


2018-12-31 05:00:00

In [22]:
print(datetime.datetime.fromisoformat('2018-12-31'))


2018-12-31 00:00:00

In [23]:
# print(datetime.datetime.fromisoformat('2018-12-31T5:00'))
# ValueError: Invalid isoformat string: '2018-12-31T5:00'

In [24]:
s = '2018-12-31T05:00:30.001000'

In [25]:
# print(datetime.date.fromisoformat(s))
# ValueError: Invalid isoformat string: '2018-12-31T05:00:30.001000'

In [26]:
# print(datetime.time.fromisoformat(s))
# ValueError: Invalid isoformat string: '2018-12-31T05:00:30.001000'

In [27]:
d = datetime.datetime.fromisoformat(s).date()

In [28]:
print(d)


2018-12-31

In [29]:
print(type(d))


<class 'datetime.date'>

In [30]:
t = datetime.datetime.fromisoformat(s).time()

In [31]:
print(t)


05:00:30.001000

In [32]:
print(type(t))


<class 'datetime.time'>

In [33]:
s = '2018-12-31T05:00:30'

In [34]:
s_basic = s.replace('-', '').replace(':', '')

In [35]:
print(s_basic)


20181231T050030

In [36]:
s = '2018-12-31T05:00:30.001000'

In [37]:
s_basic = s.split('.')[0].replace('-', '').replace(':', '')

In [38]:
print(s_basic)


20181231T050030

In [39]:
s_ex = datetime.datetime.strptime(s_basic, '%Y%m%dT%H%M%S').isoformat()

In [40]:
print(s_ex)


2018-12-31T05:00:30