Create a datetime object from a string in Python (Python 3.3)

reference

8.1 datetime Basic date type and time type (Python3.3)

Use strptime ()

Use strptime () to create a datetime object from a string.

Create a datetime object from a string

from datetime import datetime
>>> t = datetime.strptime('2014/01/01 00:01:02', '%Y/%m/%d %H:%M:%S')
>>> t.strftime('%Y/%m/%d %H:%M:%S')
'2014/01/01 00:01:02'

Create a date object from a string

As you can see in the reference URL, the date object does not have a strptime () method. Therefore, ** The following method is useless. ** **

This is an error


>>> from datetime import date
>>> t = date.strptime('2014/01/01', '%Y/%m/%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.date' has no attribute 'strptime'

To create a date object from a string

  1. Create a datetime object with strptime () of datetime

  2. Create a date object from the generated datetime object

It seems good to take the method.

>>> from datetime import datetime
>>> t = datetime.strptime('2014/01/01', '%Y/%m/%d')
>>> t.isoformat() #At this stage, the time part is initialized with 0
'2014-01-01T00:00:00'
>>> date = t.date()
>>> date.isoformat()
'2014-01-01'

Create a time object from a string

Creating a time object from a string can be done in the same way as creating a date object from a string.

>>> from datetime import datetime
>>> t = datetime.strptime('13:14:15', '%H:%M:%S')
>>> t.isoformat() #At this stage, the date part has been initialized on January 1, 1900.
'1900-01-01T13:14:15'
>>> t = t.time()
>>> t.isoformat()
'13:14:15'

Recommended Posts

Create a datetime object from a string in Python (Python 3.3)
Create a random string in Python
Create a JSON object mapper in Python
Generate a class from a string in Python
Create an instance of a predefined class from a string in Python
How to create a function object from a string
Insert an object inside a string in Python
Create a function in Python
Create a dictionary in Python
String object methods in Python
Touch a Python object from Elixir
Create a DI Container in Python
Create a pandas Dataframe from a string.
Create a binary file in Python
Create a Kubernetes Operator in Python
Conversion of string <-> date (date, datetime) in Python
Create a simple GUI app in Python
# 5 [python3] Extract characters from a character string
Create a deb file from a python package
[GPS] Create a kml file in Python
How to get a string from a command line argument in python
Timezone specification when converting a string to datetime type in python
Edit Excel from Python to create a PivotTable
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
How to embed a variable in a python string
Create a Python module
Create a C array from a Python> Excel sheet
String manipulation in python
Create SpatiaLite in Python
Create a standard normal distribution graph in Python
Create a virtual environment with conda in Python
Create a New Todoist Task from Python Script
How to generate a Python object from JSON
Object oriented in python
Create a decision tree from 0 with Python (1. Overview)
Call a Python script from Embedded Python in C ++ / C ++
Create a package containing global commands in Python
Create a Python environment
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
Behavior when saving python datetime object in MongoDB
Put out a shortened URL string in Python
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
Check if the string is a number in python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
Convert date timezone (time difference) in Python (from string)
[Python] How to expand variables in a character string
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Python script to create a JSON file from a CSV file
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Create a fake Minecraft server in Python with Quarry
How to create a kubernetes pod from python code
Take a screenshot in Python
Create a Wox plugin (Python)
[Python] Use a string sequence
Null object comparison in Python