This question already has answers here:
Parse date string and change format
(10 answers)
Closed 8 years ago.
I have two dates:
Sat Mar 15 19:47:17 +0000 2014
2014-03-12 19:50:22.159411+00:00
I need to compare these two dates but I get the error
TypeError: can't compare datetime.datetime to unicode
How should I convert one of them?
The easiest method is to use the 3rd party dateutil lib, and do:
from dateutil.parser import parse as parse_date
unicode_text = 'Sat Mar 15 19:47:17 +0000 2014'
dt = parse_date(unicode_text)
# 2014-03-15 19:47:17+00:00
if dt == other_datetime:
# do something
Related
This question already has answers here:
Convert string to datetime in python and compare to current time
(2 answers)
Closed 5 months ago.
I have this string Thu 15 Sep 2022 11:43:49 PM UTC and I want the difference in seconds between it and the current time.
How do I accomplish this in python?
You would use the datetime.strptime function from the datetime module (don't be confused, you import datetime from datetime and then call datetime.strptime--I know). You'd apply to that the correct date-string formatting.
Here's a good overview of the different formatting elements you can use with strptime.
I've made a few assumptions about zero-padding numbers (e.g., would 1 p.m. be 01:00 or 1:00?), but with that in mind, the following should work:
from datetime import datetime
datetime_str = "Thu 15 Sep 2022 11:43:49 PM UTC"
fmt = r"%a %d %b %Y %H:%M:%S %p %Z"
parsed_datetime = datetime.strptime(datetime_str, fmt)
seconds_diff = (parsed_datetime - datetime.now()).seconds
This question already has answers here:
How to print a date in a regular format?
(25 answers)
Closed 2 years ago.
How can I get the the import function to convert the date into a European date ? Example: (‘4/7/14’) returns ‘07/04/12’
import datetime
today = datetime.date.today()
print(today)
that is what datetime.strftime is for. have a look at the format codes to get your desired result.
for example:
import datetime
today = datetime.date.today()
print(today.strftime("%Y-%m-%d")) # 2020-12-02
print(today.strftime("%a %d %b %Y")) # Wed 02 Dec 2020
print(today.strftime("%d/%m/%Y")) # 02/12/2020
This question already has answers here:
datetime from string in Python, best-guessing string format
(4 answers)
How can I parse multiple (unknown) date formats in python?
(4 answers)
How can I translate dates and times from natural language to datetime? [closed]
(2 answers)
How to format date string via multiple formats in python
(5 answers)
Closed 2 years ago.
I have a problem in Python 3.9 64x bit.
In a program I am writing, I need to be able to convert any inputted date into the format %d%m%y.
For example, if the user entered 12 December 2021, the program will convert it to 121221, and if the user enters 2021 12 December, it will still convert it to 121221.
You could use pandas to_datetime and then strftime.
from pandas import to_datetime
to_datetime('12 December 2021').strftime('%d%m%y') ## returns 121221
to_datetime('2021 12 December').strftime('%d%m%y') ## returns 121221
pandas tries to infer the format when parsing the string.
Note, without specifying the datetime format for the string entered by the user there is of course ambiguity. E.g. what is meant by a the string '11/12/2021'. It could be '11 December 2021' or '12 November 2021'.
this of course is error prone if the user enters
>>> from_date="12 December 2021"
>>> import time
>>> conv=time.strptime(from_date,"%d %B %Y")
>>> time.strftime("%d/%m/%y",conv)
'121221'
>>> from_date="2021 12 December"
>>> import time
>>> conv=time.strptime(from_date,"%Y %d %B")
>>> time.strftime("%d%m%y",conv)
'121221'
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parse date string and change format
(10 answers)
Closed 2 years ago.
I have a string like this: Monday April 27 2020, 5:14pm
And I want to convert it to date time in python to become: 24-Apr-2020 5:14pm
Would python be able to do that?
from datetime import datetime
str_time = "Monday April 27 2020, 5:14pm"
formatted_time = datetime.strptime(a,"%A %B %d %Y, %I:%M%p")
new_str_representation = formatted_time.strftime("%d-%a-%Y %I:%M%p")
Converting string into datetime
https://docs.python.org/3/library/datetime.html
You can find more informations here.
This question already has answers here:
How to format date string via multiple formats in python
(5 answers)
python time string does not match format
(1 answer)
Closed 4 years ago.
I want the diff between two timestamps which are available to me as list of strings.
date_list = ['Tue Sep 25 21:12:32 PDT 2018', 'Tue Sep 25 21:15:27 PDT 2018']
How do I get the difference in minutes or in hours ?
I saw some examples, but they are using different format of UNIX timestamp. I can't change the given date format.
I suggest to use dateutil.
For instance
from dateutil import parser
d1 = parser.parse(date_list[0])
d2 = parser.parse(date_list[1])
at this point d1 and d2 are standard datetime objects and you can use the standard methods/operators to operate on them