whats the best way to convert linux date output to python datetime object?
[root#host]$ date
Wed Jun 4 19:01:58 CDT 2014
Please note there are multiple spaces between Jun and '4'
dateRaw = 'Wed Jun 4 19:01:58 CDT 2014'
sysDate = re.sub(' +',' ',dateRaw.strip())
sysDateArr = sysDate.split(' ')
sysMonth = sysDateArr[1]
sysDay = sysDateArr[2]
sysYear = sysDateArr[5]
print datetime.strptime(sysMonth+sysDay+sysYear), "%b%d%Y")
There has to be a less tedious way...
There should be no need to split everything up and rejoin it like that:
import datetime
date = 'Wed Jun 4 19:01:58 CDT 2014'
datetime.datetime.strptime(date.replace("CDT",""), '%a %b %d %H:%M:%S %Y')
should work. See the python docs[1] for all the date string parsing formats.
You can also use the python-dateutil library[2] which makes it even easier!:
from dateutil import parser
date = 'Wed Jun 4 19:01:58 CDT 2014'
parser.parse(date)
[1] https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
[2] http://labix.org/python-dateutil
Related
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")
I've spent hours on this, but my brain can't seem to figure it out, and it seems a lot of info I've found applies to Python 2?
import datetime as dt
from datetime import datetime
from pytz import timezone
import pytz
time_stamp = 'Mon, 17 Dec 2018 18:05:01 GMT'
central = timezone('US/Central')
published_time = datetime.strptime(time_stamp, '%a, %d %b %Y %H:%M:%S %Z')
published_cst = published_time.astimezone(central)
actual_time_published = published_cst.strftime('%a, %b %d %Y at %I:%M:%S %p %Z')
print(time_stamp)
print(published_time)
print(published_cst)
print(actual_time_published)
Expecting acutal_time_published to be CST because published_cst has GMT-6 hours (third result), but here is the actual result for each print command.
Mon, 17 Dec 2018 18:05:01 GMT
2018-12-17 18:05:01
2018-12-17 18:05:01-06:00
Mon, Dec 17 2018 at 06:05:01 PM CST
Pulling my hair out!
EDIT: Yes, I had "entry.published" where I meant to put "time_stamp" for the purposes of this question. Thanks for the edit!
I figured it out! Even though the original time stamp was returning a timezone (GMT), it did not have a tzinfo value (timezone value) assigned to it. It was still a "naive" datetime, even though the string contained a timezone recognized by strptime. So, I just assigned a tzinfo value of UTC to the original time and it seemed to fix it.
time_stamp = 'Mon, 17 Dec 2018 18:05:01 GMT'
utc = timezone('UTC')
central = timezone('US/Central')
published_time = datetime.strptime(time_stamp, '%a, %d %b %Y %H:%M:%S %Z')
published_gmt = published_time.replace(tzinfo=utc)
published_cst = published_gmt.astimezone(central)
actual_time_published = published_cst.strftime('%a, %b %d %Y at %I:%M:%S %p %Z')
So...
print(time_stamp)
print(published_time)
print(published_cst)
print(actual_time_published)
Yeilds this output:
Mon, 17 Dec 2018 18:05:01 GMT
2018-12-17 18:05:01
2018-12-17 18:05:01+00:00
2018-12-17 12:05:01-06:00
Mon, Dec 17 2018 at 12:05:01 PM CST
How to convert GMT time to string time? it's quite strange, when I print the time in back-end, the time is in string time format, but when I transfer it to the front-end via JSON file, the time has changed to GMT format. Why? Could someone help me?
Input: Wed, 25 Jul 2018 19:19:42 GMT
Output: 2018-07-25 19:19:42
When you convert the original time to JSON Format, it will be changed to GMT Time format. To solve this problem, you can change the time to string before converting it to JSON.
You could do something like -
from datetime import datetime
ip = 'Wed, 25 Jul 2018 19:19:42 GMT'
op = datetime.strftime(datetime.strptime(ip,'%a, %d %b %Y %H:%M:%S %Z'), '%Y-%m-%d %H:%M:%S')
# op 2018-07-25 19:19:42
Refer to this beautiful table here to get the formats right and then look into strftime and strptime
You can find resources here on strptime
from datetime import datetime
t = 'Wed, 25 Jul 2018 19:19:42 GMT'
datetime.strptime(t,'%a, %d %b %Y %H:%M:%S %Z')
datetime.datetime(2018, 7, 25, 19, 19, 42)
I have a date in a string format: "Wed, 26 Apr 2017 12:39:28 GMT" and need to convert to datetime.
To do the conversion I am using datetime.strptime
Ex:
datetime.strptime ("Wed, 26 Apr 2017 12:39:28 GMT", '% b% d% Y% I:% M% p')
However, the error:
ValueError: time data 'Wed, 26 Apr 2017 12:39:57 GMT' does not match format '% b% d% Y% I:% M% p'
If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised. You should remove the redundant spaces and modify the directive which would be used to parse the given string.
you can try this:
>>> from datetime import datetime
>>>
>>> ds="Wed, 26 Apr 2017 12:39:28 GMT"
>>>
>>> datetime.strptime (ds, '%a, %d %b %Y %H:%M:%S %Z')
datetime.datetime(2017, 4, 26, 12, 39, 28)
See more details from strftime() and strptime() Behavior
Like the error says, the string doesn't match the format. Try this:
datetime.datetime.strptime('Wed, 26 Apr 2017 12:39:28 GMT', '%a, %d %b %Y %H:%M:%S %Z')
I have a file with dates in a few different formats and am trying to get them all into YYYYMMDD format in Python. Most of the dates are in the below format:
Mon, 01 Jul 2013 16:33:59 GMT
and I have no idea how to get them into
20130701
I apologize if this is a pretty simple question---I am sort of new to python
EDIT: I am trying to do this for ANY given date. I used the 01 July as an example and in retrospect made it seem like I was asking a different question. So I guess I am looking for something that can both find dates and reformat them
Use the python-dateutil library:
from dateutil import parser
dtobject = parser.parse(datestring)
The datutil.parser.parse() method recognises a wide variety of date formats, and returns a datetime.datetime() object.
Use the datetime.strftime() method if you want to format the result as a (uniform) string again:
dtobject.strftime('%Y%m%d')
Demo:
>>> from dateutil import parser
>>> parser.parse('Mon, 01 Jul 2013 16:33:59 GMT')
datetime.datetime(2013, 7, 1, 16, 33, 59, tzinfo=tzlocal())
>>> parser.parse('Mon, 01 Jul 2013 16:33:59 GMT').strftime('%Y%m%d')
'20130701'
This can be achieved following way also:
import datetime
x = 'Mon, 01 Jul 2013 16:33:59 GMT'
''.join(str(datetime.datetime.strptime(x, '%a, %d %b %Y %H:%M:%S %Z').date()).split('-'))
if any other parameter is introduced in your date string, you can include the directive . for example %p is Locale’s equivalent of either AM or PM.