How to strip date time in python - python

From a website I'm getting a date in such format: Sun Jan 22 21:32:58 +0000 2012. I understand that I must get rid of +0000 to convert it to the date, but how exactly I can do it? I read the documentation but my code is not working:
from datetime import datetime
strDate = 'Mon Apr 29 14:30:53 2019'
objDate = datetime.strptime(strDate, '%a %b %H %M %S %Y')
I'm getting an error:
ValueError: time data 'Mon Apr 29 14:30:53 2019' does not match format '%d %m %H %M %S %Y'
And I don't really understand why. Or anyone knows how I can get a date from Sun Jan 22 21:32:58 +0000 2012?

If your object is datetime.datetime you can just simply do date()
from datetime import datetime
datetime1 = datetime.now()
date1 = datetime1.date()

One line solution:
strDate ='Sun Jan 22 21:32:58 +0000 2012'
objDate = datetime.strptime(strDate, '%a %b %d %H:%M:%S +%f %Y')
print(objDate)
#2019-04-29 14:30:53
Details:
You just forgot to use %d in order to capture the date number and the : for the time and you ALSO need to capture +0000.
Proof:

I'm afraid that the currently accepted answer, by seralouk, is incorrect. Using "+%f" turns the numbers into fractions of seconds. It's fine for 0000, but will mess things up if they happen to be anything else.
This is because the "+0000" part is a time zone offset, and the proper way to parse it is by using the "%z" directive, which will handle the "+" sign as well, so remove that from the format string:
>>> date_string = "Sun Jan 22 21:32:58 +0000 2012"
>>> datetime.strptime(date_string, "%a %b %d %H:%M:%S %z %Y")
datetime.datetime(2012, 1, 22, 21, 32, 58, tzinfo=datetime.timezone.utc)

You're missing colons : and the day format string %d. See the official documentation of strptime for a table that shows the different formatting values.
from datetime import datetime
strDate = 'Mon Apr 29 14:30:53 2019'
objDate = datetime.strptime(strDate, '%a %b %d %H:%M:%S %Y')

You can get rid of the '+0000' like this:
from datetime import datetime
strDate ='Sun Jan 22 21:32:58 +0000 2012'
objDate = datetime.strptime(strDate.replace(strDate.split(" ")[4] + " ", ""), '%a %b %d %H:%M:%S %Y')
print(objDate)
-> 2012-01-22 21:32:58
By the way,
1. The example of code that you post is not the one for the problem that you are asking about! (it does not include the +0000 in the string).
2. The error that you share (false format of the date, which has been answered already and described f.ex. here) is for another code, not the one you present above! (Error throws '%d %m %H %M %S %Y' instead of '%a %b %H %M %S %Y').

In your string u are missing the %d to catch the Day of month 01-31
'%a %b %d %H:%M:%S %Y'
If u want also to catch the +0000 u have to use the %z notation
Use this as a refrence to build your string correctly:
http://strftime.org/

Related

How to create date timestamp logs like git log

Using datetime.datetime.now(), I receive some badly formatted timestamps.
Is there an intuitive way of creating a date timestamp in this format?
Wed Aug 7 13:38:59 2019 -0500
This is seen in git log.
You can use datetime.datetime.strftime() to format dates as shown below:
from datetime import datetime
d = '2019-08-07 13:38:59-0500'
d2 = datetime.strptime(d, '%Y-%m-%d %H:%M:%S%z')
d3 = d2.strftime('%a %b %d %H:%M:%S %Y %z')
print(d3)
This returns:
Wed Aug 07 13:38:59 2019 -050000
This website is a great resource for strftime formatting.
You can still use the datetime library. Using strftime, you can rewrite the datetime object into a nicely formatted string.
In your case, you are going for Wed Aug 7 13:38:59 2019 -0500, which translated to strftime formatting is "%a %b %d %H:%M:%S %Y %z".
Overall, it'd be
datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y %z")
Which will give a string that looks like 'Wed Aug 7 13:38:59 2019 -0500'.
I would do the following:
from time import gmtime, strftime
if __name__ == "__main__":
time = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
print(time)
This was found on the documentation page of the time module. There are also a lot of additional features you might be interested in using outlined here: https://docs.python.org/3/library/time.html#time.strftime

Converting from unix time string to python date type, Does not convert when CDT is present

I have a date time string in this format in python
"Wed Mar 20 00:52:54 CDT 2019 20 Mar 2019"
I am trying to convert this to python date time format using below code
datetime.datetime.strptime('Wed Mar 20 00:52:54 CDT 2019', "%a %b %d %H:%M:%S %Y")
But it gives error as CDT is present in the text. How to handle this problem. I am not sure if CDT will be present for all dates. In both cases i want to convert.
My requirement is only to get the difference in days between this date and now
Use CDT in the format, then calculate the difference between now and that date. Finally print delta.days.
from datetime import datetime
date0 = datetime.strptime('Wed Mar 20 00:52:54 CDT 2019', "%a %b %d %H:%M:%S CDT %Y")
date_now = datetime.now()
delta = date_now - date0
print(delta.days)
Output in this case:
0
If you're not sure if there will be CDT, check the string before passing it to strptime. Or use try/except:
try:
date0 = datetime.strptime(string, "%a %b %d %H:%M:%S CDT %Y")
except ValueError:
date0 = datetime.strptime(string, "%a %b %d %H:%M:%S %Y")

Value Error in Python when converting string to date using datetime.strptime

I have a string variable named dte that prints in the following format:
Tue Feb 13 23:49:10 +0000 2018
I'm trying to convert it to a date type so that I can work out how long ago it was in days, hours, minutes etc but keep getting the following error:
ValueError: time data 'Tue Feb 13 23:49:10 +0000 2018' does not match format '%a %b %d %I:%M:%S %z %Y'
I'm using this code:
new_date = datetime.datetime.strptime(dte, "%a %b %d %I:%M:%S %z %Y")
print(new_date)
I've tried removing the colons but get the same error. I'm sure it's something very simple I'm overlooking but can't figure out what it is.
Any help appreciated.
You need %H for the hour component not %I:
In[8]:
dte="Tue Feb 13 23:49:10 +0000 2018"
new_date = dt.datetime.strptime(dte, "%a %b %d %H:%M:%S %z %Y")
new_date
Out[8]: datetime.datetime(2018, 2, 13, 23, 49, 10, tzinfo=datetime.timezone.utc)
You have 24-hour hour component, not 12-hour which is what %I is for, see the docs

Python: strptime() format-timezone field not working as expected

I'm trying to parse a given string of custom format date into epoch time with the following code:
time_tuple = time.strptime('Tue Dec 05 13:01:48 PST 2017', '%a %b %d %X %Z %Y')
time_epoch = time.mktime(time_tuple)
The given date format is as shown in the code. The problem I'm facing is when the code is compile I keep getting error saying:
ValueError: time data 'Tue Dec 05 13:01:48 PST 2017' does not match format '%a %b %d %X %Z %Y'
From what I can see, the format seems to be exactly as specified according to: https://docs.python.org/3/library/datetime.html
What seem's to be wrong?
Edit:
I have tried using datetime instead of time as well, but didn't work
time_tuple = datetime.strptime('Tue Dec 05 13:01:48 PST 2017', '%a %b %d %X %Z %Y')
Based on the comments below, it seems like the timezone field(%Z) is causing the problem. It seems like that field is based on the timezone the code is ran, and not the timezone the input string gives.
You are using a different time zone than those described in the docs, however, if you change the time zone, you can try this:
import time
time_tuple = time.strptime('Tue Dec 05 13:01:48 EST 2017', '%a %b %d %X %Z %Y')
time_epoch = time.mktime(time_tuple)

ValueError parsing time string

I have written this code to convert a unusual time into EPOCH:
x = 'Mon Jul 25 19:04:30 GMT+01:00 2016'
print(datetime.strptime(x, '%a %b %d %H:%M:%S %Z%z %Y').strftime('%s'))
However, it returns the error ValueError: time data 'Mon Jul 25 19:04:30 GMT+01:00 2016' does not match format '%a %b %d %H:%M:%S %Z%z %Y'
The problem is something to do with the timezone. What have I done wrong?
Your timezone format has an extra : inside which causes the format mismatching error, you can remove the last : from the string firstly and then parse it:
import re
from datetime import datetime
x1 = re.sub(r":(?=[^:]+$)", "", x) # remove the last semi colon
datetime.strptime(x1, '%a %b %d %H:%M:%S %Z%z %Y').strftime('%s')
# '1469487870'
If you use dateutil instead of datetime.strptime it seems to work:
from dateutil import parser
parser.parse("Mon Jul 25 19:04:30 GMT+01:00 2016")
>> datetime.datetime(2016, 7, 25, 19, 4, 30, tzinfo=tzoffset(None, -3600))

Categories

Resources