In [1]:
import datetime
In [2]:
s = '2018-12-31'
In [3]:
d = datetime.date.fromisoformat(s)
In [4]:
print(d)
In [5]:
print(type(d))
In [6]:
# print(datetime.date.fromisoformat('2018-12'))
# ValueError: Invalid isoformat string: '2018-12'
In [7]:
print(datetime.date.fromisoformat('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)
In [12]:
print(type(t))
In [13]:
print(datetime.time.fromisoformat('05'))
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)
In [18]:
print(type(dt))
In [19]:
print(datetime.datetime.fromisoformat('2018-12-31x05: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'))
In [22]:
print(datetime.datetime.fromisoformat('2018-12-31'))
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)
In [29]:
print(type(d))
In [30]:
t = datetime.datetime.fromisoformat(s).time()
In [31]:
print(t)
In [32]:
print(type(t))
In [33]:
s = '2018-12-31T05:00:30'
In [34]:
s_basic = s.replace('-', '').replace(':', '')
In [35]:
print(s_basic)
In [36]:
s = '2018-12-31T05:00:30.001000'
In [37]:
s_basic = s.split('.')[0].replace('-', '').replace(':', '')
In [38]:
print(s_basic)
In [39]:
s_ex = datetime.datetime.strptime(s_basic, '%Y%m%dT%H%M%S').isoformat()
In [40]:
print(s_ex)