Conversion of string <-> date (date, datetime) in Python

From string to date (datetime)

from datetime import datetime as dt

tstr = '2012-12-29 13:49:37'
tdatetime = dt.strptime(tstr, '%Y-%m-%d %H:%M:%S')

The second argument of strptime passes the format of the first argument. For example If tstr = '2012/12/29 13:49:37', dt.strptime(tstr, '%Y/%m/%d %H:%M:%S')

From string to date

import datetime

tstr = '2012-12-29 13:49:37'
tdatetime = datetime.datetime.strptime(tstr, '%Y-%m-%d %H:%M:%S')
tdate = datetime.date(tdatetime.year, tdatetime.month, tdatetime.day)

String from date

from datetime import datetime as dt

tdatetime = dt.now()
tstr = tdatetime.strftime('%Y/%m/%d')

reference If you want to know other specifiers such as week (like% Y), please check from the bottom of the link below Basic date and time type

Recommended Posts

Conversion of string <-> date (date, datetime) in Python
String date manipulation in Python
Summary of date processing in Python (datetime and dateutil)
Comparison of Japanese conversion module in Python3
String → Bool value conversion in Python Consideration
String manipulation in python
Date manipulation in Python
[Python2] Date string-> UnixTime-> Date string
python unix-time <-> datetime conversion
Get date in Python
Date calculation in python
Date calculation in Python
Convert "number" of excel date to python datetime
Create a datetime object from a string in Python (Python 3.3)
[Python] How to change character string (str) data to date (strptime of datetime)
[python] Convert date to string
String object methods in Python
Convert date timezone (time difference) in Python (from string)
Store Japanese (multibyte character string) in sqlite3 of python
Equivalence of objects in Python
[python] Calculation of months and years of difference in datetime
Implementation of quicksort in Python
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Sort by date in python
Find out the apparent width of a string in python
Pixel manipulation of images in Python
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Implementation of life game in Python
Waveform display of audio in Python
Handling timezones in Python (datetime, pytz)
6 ways to string objects in Python
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Create a random string in Python
Specify the encoding of the unicode string in the Python 2.x print statement
Create an instance of a predefined class from a string in Python
Timezone specification when converting a string to datetime type in python
Python string
Get the title and delivery date of Yahoo! News in Python
Check the behavior of destructor in Python
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Output tree structure of files in Python
(Java, JavaScript, Python) Comparison of string processing
Display a list of alphabets in Python 3
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
Summary of various for statements in Python
python string comparison / use'list'and'in' instead of'==' and'or'
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python
Receive date type (datetime) with ArgumentParser [python]
String conversion of a list containing numbers
The basics of running NoxPlayer in Python
Basic grammar of Python3 system (character string)
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Conversion of time data in 25 o'clock notation