Convert date to datetime in Python
You can use datetime.combine(date, time)
; for the time, you create a datetime.time
object initialized to midnight.
from datetime import date
from datetime import datetime
dt = datetime.combine(date.today(), datetime.min.time())
Python: most efficient way to convert date to datetime
Use datetime.datetime.combine()
with a time object, datetime.time.min
represents 00:00
and would match the output of your date-string-datetime path:
datetime.datetime.combine(my_date, datetime.time.min)
Demo:>>> import datetime
>>> my_date = datetime.date.today()
>>> datetime.datetime.combine(my_date, datetime.time.min)
datetime.datetime(2013, 3, 27, 0, 0)
How to convert a date string to different format
I assume I have import datetime
before running each of the lines of code below
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
prints "01/25/13"
.If you can't live with the leading zero, try this:
dt = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print '{0}/{1}/{2:02}'.format(dt.month, dt.day, dt.year % 100)
This prints "1/25/13"
.EDIT: This may not work on every platform:
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
Python convert date to datetime
You can use datetime
module like this example:
import datetime
date = "2018-05-08"
final = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%dT%H:%M:%S.%f')
print(final)
Output:2018-05-08T00:00:00.000000
For more details visit datetime
's documentation 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'
how to convert datetime.date to datetime.datetime?
You can either check if dates are the same by taking date from the datetime
object, or add default time (00:00:00) to the date
object to make it datetime
:
from datetime import datetime, date
dt = datetime.fromisoformat("2020-01-01T10:00:00")
d = date.fromisoformat("2020-01-01")
print(dt, d) # 2020-01-01 00:00:00 2020-01-01
print(type(dt), type(d)) # <class 'datetime.datetime'> <class 'datetime.date'>
# Check only by date, do not check time
if d == dt.date():
print("Same date")
# Check by date, but both have to be 00:00:00
d = datetime.fromisoformat(d.isoformat())
if d == dt:
print("Same date and 00:00:00")
Related Topics
How to Convert Datetime.Timedelta to Minutes, Hours in Python
Setting Up S3 for Logs in Airflow
Crontab Not Executing a Python Script
Reading Tar File Contents Without Untarring It, in Python Script
Selecting Columns from Pandas Multiindex
Removing Time from Date&Time Variable in Pandas
Pycharm: Set Environment Variable for Run Manage.Py Task
Extracting Days from a Numpy.Timedelta64 Value
Python Insert Numpy Array into SQLite3 Database
Python Pip on Windows - Command 'Cl.Exe' Failed
Python - Legend Overlaps with the Pie Chart
What Does Model.Train() Do in Pytorch
Typeerror: Got Multiple Values for Argument
Syntaxerror: Unexpected Eof While Parsing
How to Get All the Request Headers in Django