This question already has answers here:
How to convert local time string to UTC?
(25 answers)
datetime from string in Python, best-guessing string format
(4 answers)
Closed 11 months ago.
I am converting a datetime object to string using:
from datetime import datetime
dt = datetime.now()
dt_str = str(dt)
# Now I want to get `dt`(datetime object) back from `dt_str`
How to convert dt_str to dt?
Can anyone help me with this?
Use strptime method. Example:
from datetime import datetime
date_string = "2020-03-14"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
%Y - Year in four digits. Example: 2018, 2019 etc.
%m - Month as a zero-padded decimal number. Example: 01, 02, ..., 12
%d - Represents the day of the month. Example: 01, 02, ..., 31
Related
This question already has answers here:
Convert date to datetime in Python
(14 answers)
Closed 2 years ago.
In Python, I convert a date to datetime by:
converting from date to string
converting from string to datetime
Code:
import datetime
dt_format="%d%m%Y"
my_date = datetime.date.today()
datetime.datetime.strptime(my_date.strftime(dt_format), dt_format)
I suspect this is far from the most efficient way to do this. What is the most efficient way to convert a date to datetime in Python?
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)
Alternatively, as suggested here, this might be more readable:
datetime(date.year, date.month, date.day)
This question already has answers here:
Parse date string and change format
(10 answers)
Closed 4 years ago.
i have string
date = "2018-09-12"
i want to get output like 2018-September-12
and i try like this
from datetime import datetime
date3 = datetime.strptime(date, '%Y-%m%B-%d')
or date3 = datetime.strptime(date, '%Y-%B-%d')
but always get time data '2018-09-12' does not match format '%Y-%m%B-%d'
Use strftime
Ex:
from datetime import datetime
date = "2018-09-12"
date3 = datetime.strptime(date, '%Y-%m-%d').strftime("%Y-%B-%d")
print(date3)
Output:
2018-September-12
strptime to convert string datetime to datetime object.
strftime to convert datetime object to required string format.
This question already has answers here:
How do I translate an ISO 8601 datetime string into a Python datetime object? [duplicate]
(11 answers)
Closed 5 years ago.
I have DateTime String in this format "2018-02-08T23:59:05.823Z" I know 'T ' is separating date from time, I have split the string on 'T' delimiter and I got Time - > '23:59:05.823Z'
I want to convert this time '23:59:05.823Z' to this format like '10:00 AM'
How can we do that in python ?
I want to convert this time '23:59:05.823Z' to this format like '10:00
AM'
looks like you are using ISO date time format. You could do something like this.
>>> from datetime import datetime
>>> d = datetime.strptime("23:59:05.823Z", "%H:%M:%S.%fZ")
>>> d.strftime("%I:%M %p")
'11:59 PM'
If you are using the Python datetime module then you can use the .strftime() method to convert you datetime into a string of your desired format. For example:
from datetime import datetime
current_datetime = datetime.now()
print(current_datetime)
>>> 2018-02-17 09:23:31.079326
print(current_datetime.strftime("%I:%M %p")
>>> 09:23 AM
%I gives you the hour in a 12-hour clock as a zero-padded number, %M gives you the minute as a zero-padded number, and %p gives you AM or PM. You can read more about this in the official documentation: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
How do I parse an HTTP date-string in Python?
(4 answers)
Closed 5 years ago.
I need count days to password change in linux.
I know how to do this in python e.g:
>>> import datetime
>>> start = datetime.date(2016,1,1)
>>> end = datetime.date(2016,2,28)
>>> end-start
But my date format is:
Oct 03, 2017
How I can calculate days to a date?
You can do this using strftime().
It is actually really simple:
from datetime import datetime
d1 = datetime.strptime("Jan 01, 2016", '%b %d, %Y')
d2 = datetime.strptime("Feb 28, 2016", '%b %d, %Y')
print "Delta (in days):", (d2-d1).days
And you'll get 58 as a result.
This question already has answers here:
Python date string to date object
(9 answers)
Closed 9 years ago.
How do I convert a string to a date object in python?
The string would be: "30-01-12" (corresponding to the format: "%d-%m-%y")
I don't want a datetime.datetime object, but rather a datetime.date
You still use datetime.datetime but then request just the .date() portion:
datetime.datetime.strptime('30-01-12', '%d-%m-%y').date()
Demonstration:
>>> import datetime
>>> datetime.datetime.strptime('30-01-12', '%d-%m-%y').date()
datetime.date(2012, 1, 30)
This should work:
import datetime
s = "30-01-12"
slist = s.split("-")
sdate = datetime.date(int(slist[2]),int(slist[0]),int(slist[1]))
from datetime import datetime,date
date_str = '30-01-12'
formatter_string = "%d-%m-%y"
datetime_object = datetime.strptime(date_str, formatter_string)
date_object = datetime_object.date()