I have a time String like this:
07/01/2015-14:31:58.520
I use this command line to convert it:
import time
timeStr = "07/01/2015-14:31:58.520"
time.strptime(timeStr,'%d/%m/%y-%H:%M:%S.%f')
But this returns:
ValueError: time data '07/01/2015-14:31:58.520' does not match format
'%d/%m/%y-%H:%M:S.%f'
My python version is 2.7.7
%y denotes a 2 digit year, but your string has a 4 digit year. Use %Y (capital Y) to denote a 4 digit year. See the docs for more information.
time.strptime(timeStr, '%d/%m/%Y-%H:%M:%S.%f')
Note that datetime.strptime may be more useful, as it will return a full datetime object rather than a tuple. The format syntax is essentially the same.
It should have been capital Y for year (%Y in place of %y)
time.strptime(timeStr,'%d/%m/%Y-%H:%M:%S.%f')
You need to use %Y instead of %y
time.strptime(timeStr,'%d/%m/%Y-%H:%M:%S.%f')
To get a datetime object, use python-dateutil
To install
pip install python-dateutil
Then
t = "07/01/2015-14:31:58.520"
from dateutil import parser
>>>parser.parse(t)
datetime.datetime(2015, 7, 1, 14, 31, 58, 520000)
tim = parser.parse(t)
>>>str(tim.date())
'2015-07-01'
All operations to datetime objects is possible.
the time.strptime syntax %d/%m/%y-%H:%M:%S.%f is incorrect, it should be
"%d/%m/%Y-%H:%M:%S.%f"
where the only difference is that %y has become %Y. The reason is because from the docs %y is without century number ( [00,99] ), whereas %Y is with century number, which is the syntax you use with "2015"
Tested and functinal in python 2.7.5 and 3.4.1
Edit: Zero answers when I started typing this, 6 answers by time of post, sorry about that!
Edit #2: datetime.strptime functions similarly, so if you want to use that as well, you can!
Related
I know there are a lot of answers to this question online, but none of them have worked for me. I am trying to convert a date string into a Datetime object, of the following format: yyyy-mm-dd
My date_string is '2017-02-02T00:00:00Z'
I am trying to convert it by doing date_value = datetime.datetime.strptime(date_string, '%Y%m%d') but I'm getting the following error:
ValueError: time data '"2017-02-02T00:00:00Z"' does not match format
'%Y%m%d'
Also, should I be worried about the double quotes around my date_string string?
The second argument in the method strptime is the pattern of your string.
Here is the full list of available code formats. https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
All the remaining "non-informative" characters in your string can simply be put as-is in there correct places.
Thanks to #MrFuppes for this info: you should also parse the trailing "Z" as %z. This will signal python that it's a UTC datetime and not a local datetime.
Your code should be :
date_string = '2017-02-02T00:00:00Z'
date_value = datetime.datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S%z')
As for the extra quotes, that's not wanted. You should try this beforehand :
date_string = date_string.strip("'").strip('"')
If strip() didn't work, you can call eval instead (usually not recommended) :
date_string = eval(date_string)
The solution is to parse your date_string first, and that should help. Using strptime() right away on an unparsed datetime string can sometimes cause problems. Also you shouldn't worry about your double quotes, it's fine.
First, install the python-dateutil library if you haven't already (pip install python-dateutil at the command line). Then test the solution with the following code.
import datetime
import dateutil.parser
date_string = '2017-02-02T00:00:00Z'
#we parse the string, it becomes a datetime object
parsed_date_string = dateutil.parser.parse(date_string)
print(parsed_date_string)
#output looks like this: 2017-02-02 00:00:00+00:00
#now your statement will work
date_value = datetime.datetime.strptime(str(parsed_date_string), '%Y-%m-%d %H:%M:%S%z')
print(date_value)
#output will also be: 2017-02-02 00:00:00+00:00
The strptime() statement worked this time because we parsed our date first with parse(). Note also that to use strptime() we need to cast our parsed_date_string back to a string because parse() converts our original string to an object of class datetime.datetime and strptime() is expecting a string.
Hopefully that helped.
This question already has answers here:
Python 2.7 how parse a date with format 2014-05-01 18:10:38-04:00 [duplicate]
(2 answers)
Closed 6 years ago.
I am receiving a json that prints time data '2016-04-15T02:19:17+00:00' I I cant seem to figure out the format of this unicode string.
I need to find a difference in time between then and now. The first step in that is to convert the string to structured format and Iam not able to find the format
fmt='"%Y-%m-%d %H:%M:%S %Z'
#fmt='%Y-%m-%d %H:%M:%S.%f'
print datetime.datetime.strptime(result_json['alert_time'], fmt)
I keep getting exception that it is not the same format
time data '2016-04-15T02:19:17+00:00' does not match format '"%Y-%m-%d %H:%M:%S %Z'
There are a few problems with your format. First, it has a double quote " in it. Second, you need to include the T between the date and the time. Third, the timezone offset is not standard. Here is code that will work:
print datetime.datetime.strptime('2016-04-15T02:19:17', '%Y-%m-%dT%H:%M:%S')
If your alert_time is always in GMT, you can just trim the timezone off before calling strptime.
The answer by Brent is the safer and faster option rather than having things going on under the hood. But the amount of times I've had datetime as a frustrating bottleneck not associated with the main problem I wanted to test out, I will also point out that dateparser here has not yet been wrong for me and will take a huge range of inputs.
import dateparser
import datetime
date = '2016-04-15T02:19:17+00:00'
date_parser_format = dateparser.parse(date)
datetime_format = datetime.datetime.strptime('2016-04-15T02:19:17', '%Y-%m-%dT%H:%M:%S')
print date_parser_format
print datetime_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 7 years ago.
How do I modify the code below to handle a timezone, note there is no decimal.
2015-12-22T11:57:11-08:00, -8:00 is causing me issues, does epoch time take time zone into account?
timegm(datetime.strptime(datestring, "%Y-%m-%dT%H:%M:%S.%f").timetuple())
There are a few issues here.
If your time doesn't include a microsecond block, this date string will not work with the format string you provided. Just try it without the -08:00 bit. That means you either need to assume that all your times won't have that block or you need to account for both possibilities.
strptime does a TERRIBLE job of dealing with ISO8601 offsets. If you look at the formatting guide, you'll notice that you can use %z for +/-HHMM, but ISO8601 time zones (in my experience) are almost always presented with the format +/-HH:MM. And even then %z has a nasty habit of being called a bad directive in the format string.
To answer your question, yes, time zone matters. The UNIX epoch is seconds since 1970-01-01T00:00:00+00:00. More importantly, even if you correctly assign the datetime object's tzinfo when you parse the string, timetuple will NOT take into account that tzinfo. You need to use utctimetuple
So now you just need to properly parse the datetime. There are solutions that don't use external libraries, but I find the easiest way to parse ISO8601 date strings is to use the python-dateutil package available via pip:
>>> import calendar
>>> from dateutil import parser
>>> datestring = '2015-12-22T11:57:11-08:00'
>>> tz_aware_datetime = parser.parse(datestring)
>>> tz_aware_datetime
datetime.datetime(2015, 12, 22, 11, 57, 11, tzinfo=tzoffset(None, -28800))
>>> calendar.timegm(tz_aware_datetime.utctimetuple())
1450814231
There is a datetime string that I would like to convert back into a date. The time zone is giving me trouble and I don't know how to solve it.
datetime.datetime.strptime(json_event['date_time'], '%a, %d %b %Y %H:%M:%S %Z')
I get the error message:
ValueError: time data 'Tue, 08 Apr 2014 17:57:34 -0000' does not match
format '%a, %d %b %Y %H:%M:%S %Z'
If I leave %Z out, I get this error message:
ValueError: unconverted data remains: -0000
The date is originally a UTC:
current_date = datetime.datetime.utcnow()
UPDATE:
I would like to solve this natively without any external libraries such as dateutil.parser, hence the solution in the duplicate doesn't help me.
import dateutil.parser
date = dateutil.parser.parse(json_event['date_time'])
If you don't have dateutil, get it.
pip install python-dateutil
If you are always getting UTC times: Ignore the last 6 chars (space, sign, 4 digts) and then convert to datetime as you've done without the %Z.
One issue you'll have is that your system will assume that it is your local timezone and if you convert it to any other timezone, it will convert wrongly. In that case, next step is to use this answer from another question.
If you get non-UTC times as well:
crop out the last 6 chars.
Do the strptime on the last 4 digits, with the format HHMM (%H%M) --> Y
Get the sign and reverse in step 5 below.
Then get the rest of the datetime as you have above (leaving those last 6 chars and no %Z in the format) --> X
Then X-Y (or X+Y, invert what is got from step 3) will give you a datetime object. Then follow the steps in the linked answer to make the datetime obj timezone aware.
I have a text file with a lot of datetime strings in isoformat. The strings are similar to this:
'2009-02-10 16:06:52.598800'
These strings were generated using str(datetime_object). The problem is that, for some reason, str(datetime_object) generates a different format when the datetime object has microseconds set to zero and some strings look like this:
'2009-02-10 16:06:52'
How can I parse these strings and convert them into a datetime object?
It's very important to get all the data in the object, including microseconds.
NOTE: I have to use Python 2.5, the format directive %f for microseconds doesn't exist in 2.5.
Alternatively:
from datetime import datetime
def str2datetime(s):
parts = s.split('.')
dt = datetime.strptime(parts[0], "%Y-%m-%d %H:%M:%S")
return dt.replace(microsecond=int(parts[1]))
Using strptime itself to parse the date/time string (so no need to think up corner cases for a regex).
Use the dateutil module. It supports a much wider range of date and time formats than the built in Python ones.
You'll need to easy_install dateutil for the following code to work:
from dateutil.parser import parser
p = parser()
datetime_with_microseconds = p.parse('2009-02-10 16:06:52.598800')
print datetime_with_microseconds.microsecond
results in:
598799
Someone has already filed a bug with this issue: Issue 1982. Since you need this to work with python 2.5 you must parse the value manualy and then manipulate the datetime object.
It might not be the best solution, but you can use a regular expression:
m = re.match(r'(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(?:\.(\d{6}))?', datestr)
dt = datetime.datetime(*[int(x) for x in m.groups() if x])