Python date string to date object
You can use strptime
in the datetime
package of Python:
>>> import datetime
>>> datetime.datetime.strptime('24052010', "%d%m%Y").date()
datetime.date(2010, 5, 24)
Convert datetime object to a String of date only in Python
You can use strftime to help you format your date.
E.g.,
import datetime
t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')
will yield:
'02/23/2012'
More information about formatting see here
Converting string to date object without time info
The object that is returned by datetime.datetime.strptime
is a datetime.datetime
object rather than a datetime.date
object. As such it will have the time information included and when isoformat()
is called will include this information.
You can use the datetime.datetime.date()
method to convert it to a date
object as below.
import datetime as dt
d = dt.datetime.strptime("25-01-1973", "%d-%m-%Y")
# Convert datetime object to date object.
d = d.date()
print(d.isoformat())
# 1973-01-25
Convert string into Date type on Python
You can do that with datetime.strptime()
Example:
>>> from datetime import datetime
>>> datetime.strptime('2012-02-10' , '%Y-%m-%d')
datetime.datetime(2012, 2, 10, 0, 0)
>>> _.isoweekday()
5
You can find the table with all the strptime
directive here.
To increment by 2 days if .isweekday() == 6
, you can use timedelta()
:
>>> import datetime
>>> date = datetime.datetime.strptime('2012-02-11' , '%Y-%m-%d')
>>> if date.isoweekday() == 6:
... date += datetime.timedelta(days=2)
...
>>> date
datetime.datetime(2012, 2, 13, 0, 0)
>>> date.strftime('%Y-%m-%d') # if you want a string again
'2012-02-13'
Convert date string format to a datetime Python Object
from datetime import datetime
dates = {"date":"2020-08-24T21:15:00+00:00"}
date = dates.get("date")
day = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S+00:00")
Your looking for strptime.
Heres a good article:
https://www.programiz.com/python-programming/datetime/strptime
Python convert date format
An alternative approach using pandas function below:
import pandas as pd
d = pd.to_datetime('2020-08-14')
d.strftime('%d %B %Y')
Out[11]: '14 August 2020'
Parse date string and change format
datetime
module could help you with that:
datetime.datetime.strptime(date_string, format1).strftime(format2)
For the specific example you could do
>>> import datetime
>>> datetime.datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')
'15/02/2010'
>>>
Related Topics
Why Is Dictionary Ordering Non-Deterministic
How to Read Realtime Microphone Audio Volume in Python and Ffmpeg or Similar
Extract Text from Xml Documents in Python
Why Is Equivalent Python Code So Much Slower
Parsing Date/Time String with Timezone Abbreviated Name in Python
Quick and Easy File Dialog in Python
How to Zip Two Differently Sized Lists, Repeating the Shorter List
Python's Most Efficient Way to Choose Longest String in List
Merge Lists That Share Common Elements
Index N Dimensional Array with (N-1) D Array
Is Shared Readonly Data Copied to Different Processes for Multiprocessing
How to Execute Python File in Linux
Numpy Selecting Specific Column Index Per Row by Using a List of Indexes
What Is the Fastest Way to Flatten Arbitrarily Nested Lists in Python