Convert string object to date object in Python [duplicate] - python

This question already has answers here:
Convert string into Date type on Python [duplicate]
(5 answers)
Closed 2 years ago.
I have a string object
value="2020-02-28"
and want output as a date object in python.
Used
datetime_value= datetime.strptime((str(value), "%b%d, %Y"))
But it does not work.

Make these minor changes :
from _datetime import datetime
value="2020-02-28"
datetime_value= datetime.strptime(value, "%Y-%m-%d")
print(datetime_value, type(datetime_value))
# 2020-02-28 00:00:00 <class 'datetime.datetime'>

First, "it does not work" is really not helpful in any way, shape or form. When you request help, providing the actual feedback you get (e.g. error message, traceback, ...) is significantly more actionable than providing... nothing.
Second, the format string passed to strptime (second parameter) is supposed to match the actual date, that means the placeholders should parse the fields you're trying to match, and the not-placeholders should match the not-fields.
Here your date is {year}-{month}-{day-of-month}, but the format string %b%d, %Y stands for {Month as locale’s abbreviated name}{Day of the month}, {Year}. None of the separators, fields or order match.
What you want is %Y-%m-%d.
Third, value is a string, why are you converting it to a string again?

Related

Convert a timezone aware string to datetime python which is in format like '2012-11-01T04:16:13.000Z' [duplicate]

This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 1 year ago.
I have stumbled upon a coding question where I have to convert a timezone-aware string like 2021-11-01T02:08:13.000Z to a Python datetime object.
I have seen many examples where timezone aware string is like in the format 2012-11-01T04:16:13-04:00, but for me as you can see the string is little different with the ".000Z" at the end.
I tried the below code:
from datetime import datetime
format = '%Y-%m-%dT%H:%M:%S%z'
time='2012-11-01T04:16:13.000Z'
date_time_obj = datetime.strptime(time, format)
print ("date",date_time_obj)
But I'm getting the error:
ValueError: time data '2012-11-01T04:16:13.000Z' does not match format '%Y-%m-%dT%H:%M:%S%z'
also tried doing like this:
from datetime import datetime
time='2012-11-01T04:16:13.000Z'
date_time_obj = datetime.fromisoformat(time)
But I'm getting this error:
Errpr: Invalid ISO format string:
I gone through many pages but was unable to find that particular format and how to convert it to a datetime aware string. Can anyone please help.
You can do all of this using 'dateutil.parser.isoparse'. No need to strip out information (dropping the milliseconds), no need to fuss with the formatting string.
from dateutil import parser
time='2012-11-01T04:16:13.000Z'
date_time_obj = parser.isoparse(time)
print ("date",date_time_obj)
> date 2012-11-01 04:16:13+00:00
You may change your format like this:
format = '%Y-%m-%d %H:%M:%S'
And convert time like you format by doing this:
time='2012-11-01T04:16:13.000Z'
time=time.replace("T"," ").split(".")[0]
Or you may format it like
format = '%Y-%m-%dT%H:%M:%S.%f'
And you don't have to do anything for time now.
time='2012-11-01T04:16:13.000Z'

How to require a timestamp to be zero-padded during validation in Python?

I'm trying to validate a string that's supposed to contain a timestamp in the format of ISO 8601 (commonly used in JSON).
Python's strptime seems to be very forgiving when it comes to validating zero-padding, see code example below (note that the hour is missing a leading zero):
>>> import datetime
>>> s = '1985-08-23T3:00:00.000'
>>> datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
datetime.datetime(1985, 8, 23, 3, 0)
It gracefully accepts a string that's not zero-padded for the hour for example, and doesn't throw a ValueError exception as I would expect.
Is there any way to enforce strptime to validate that it's zero-padded? Or is there any other built-in function in the standard libs of Python that does?
I would like to avoid writing my own regexp for this.
There is already an answer that parsing ISO8601 or RFC3339 date/time with Python strptime() is impossible: How to parse an ISO 8601-formatted date?
So, to answer you question, no there is no way in the standard Python library to reliable parse such a date.
Regarding the regex suggestions, a date string like
2020-14-32T45:33:44.123
would result in a valid date. There are lots of Python modules (if you search for "iso8601" on https://pypi.python.org), but building a complete ISO8601 Validator would require things like leap seconds, the list of possible time zone offset values and many more.
To enforce strptime to validate leading zeros for you you'll have to add your own literals to Python's _strptime._TimeRE_cache. The solution is very hacky, most likely not very portable, and requires writing a RegEx - although only for the hour part of a timestamp.
Another solution to the problem would be to write your own function that uses strptime and also converts the parsed date back to a string and compares the two strings. This solution is portable, but it lacks for the clear error messages - you won't be able to distinguish between missing leading zeros in hours, minutes, seconds.
You said you want to avoid a regex, but this is actually the type of problem where a regex is appropriate. As you discovered, strptime is very flexible about the input it will accept. However, the regex for this problem is relatively easy to compose:
import re
date_pattern = re.compile(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}')
s_list = [
'1985-08-23T3:00:00.000',
'1985-08-23T03:00:00.000'
]
for s in s_list:
if date_pattern.match(s):
print "%s is valid" % s
else:
print "%s is invalid" % s
Output
1985-08-23T3:00:00.000 is invalid
1985-08-23T03:00:00.000 is valid
Try it on repl.it
The only thing I can think of outside of messing with Python internals is to test for the validity of the format by knowing what you are looking for.
So, if I garner it right, the format is '%Y-%m-%dT%H:%M:%S.%f' and should be zero padded.
Then, you know the exact length of the string you are looking for and reproduce the intended result..
import datetime
s = '1985-08-23T3:00:00.000'
stripped = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
try:
assert len(s) == 23
except AssertionError:
raise ValueError("time data '{}' does not match format '%Y-%m-%dT%H:%M:%S.%f".format(s))
else:
print(stripped) #just for good measure
>>ValueError: time data '1985-08-23T3:00:00.000' does not match format '%Y-%m-%dT%H:%M:%S.%f

How do I find the date format ? [duplicate]

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

Python - Getting the date format [duplicate]

This question already has answers here:
How to determine appropriate strftime format from a date string?
(4 answers)
Closed 5 years ago.
I'm getting a date as a string, then I'm parsing it to datetime object.
Is there any way to check what's is the date format of the object?
Let's say that this is the object that I'm creating:
modified_date = parser.parse("2015-09-01T12:34:15.601+03:00")
How can i print or get the exact date format of this object, i need this in order to verify that it's in the correct format, so I'll be able to to make a diff of today's date and the given date.
I had a look in the source code and, unfortunately, python-dateutil doesn't expose the format. In fact it doesn't even generate a guess for the format at all, it just goes ahead and parses - the code is like a big nested spaghetti of conditionals.
You could have a look at dateinfer which looks to be what you're searching for, but these are unrelated libraries so there is no guarantee at all that python-dateutil will parse with the same format that dateinfer suggests.
>>> from dateinfer import infer
>>> s = "2015-09-01T12:34:15.601+03:00"
>>> infer([s])
'%Y-%d-%mT%I:%M:%S.601+%m:%d'
Look at that .601. Close but not cigar. I think it has probably also mixed up the month and the day. You might get better results by giving it more than one date string to base the guess upon.
i need this in order to verify that it's in the correct format
If you know the expected time format (or a set of valid time formats) then you could just parse the input using it: if it succeeds then the time format is valid (the usual EAFP approach in Python):
for date_format in valid_date_formats:
try:
return datetime.strptime(date_string, date_format), date_format
except ValueError: # wrong date format
pass # try the next format
raise ValueError("{date_string} is not in the correct format. "
"valid formats: {valid_date_formats}".format(**vars()))
Here's a complete code example (in Russian -- ignore the text, look at the code).
If there are many valid date formats then to improve time performance you might want to combine them into a single regular expression or convert the regex to a deterministic or non-deterministic finite-state automaton (DFA or NFA).
In general, if you need to extract dates from a larger text that is too varied to create parsing rules manually; consider machine learning solutions e.g., a NER system such as webstruct (for html input).

change date format in django

I'me trying to use this eventCalendar in Django, which saves and shows dates in this format:
2012-02-27T13:15:00.000+10:00
but when I save events in the database, they're saved in this format:
Mon Feb 27 2012 13:15:00 GMT+0330 (Iraq Standard Time)
so events from the database won't appear on the calendar because of this format. How can I convert this format?
I tried some thing like this:
datetime.strptime(mydatetime, "%Y-%m-%dT%H:%M:%S+0000")
but I'm repeatedly getting errors like this:
'module' object has no attribute 'strptime'
Edit:date is in string format
strptime is used to parse a string into a datetime object. The format string indicates how to parse the string, not the format you want the datetime to take when later printed as a string. So first off you need to make the format string match the date format of the input string.
Once you've gotten a datetime from strptime, you can then use strftime with your current format string to get it into the display you want.
That said, though, it appears you've got a problem with your imports. The error seems to indicate that you've done:
import datetime
datetime.strptime(...)
That's incorrect. strptime and strftime are methods off datetime.datetime, so you need to either modify your import like:
from datetime import datetime
Or, modify your call to strptime like:
datetime.datetime.strptime(...)
UPDATE
You're starting off with a string like Mon Feb 27 2012 13:15:00 GMT+0330 (Iraq Standard Time). Python is pretty awesome, but it's not omniscient; if you want to convert this to a datetime you have to tell it how. That's the purpose of the format string you pass to strptime. You need to create a format string that represents your current string date and time as represented in the database (exercise left to reader). Think in reverse, along the lines of it you wanted to actually represent a datetime like that, how would you do it.
This will net you a datetime. From there, you can now format that datetime as a string with strftime, passing the actual format you want, this time.
So the process is:
Create a format string representing your current string from the database
Use that format string as an argument to strptime to get a datetime
Create a format string representing the format you want the date to be in (already done)
Use that format string as the argument to strftime to convert the datetime from step 2 to your desired string.

Categories