Unable to parse the strptime in python - python

I am converting the datetime into time. My JSON datetime format is "2017-01-02T19:00:07.9181202Z". I have placed my code below:
from datetime import datetime
date_format = datetime.strptime('2017-01-02T19:00:07.9181202Z', '%Y-%m-%dT%H:%M:%S.%fZ')
time = date_format.strftime("%I:%M %p")
print(time)
Error message as below:
After that I read this python date-time document. It says that microsecond digit should be 6. But, JSON date-time microsecond has 7 digit.
Message from Python document:
%f is an extension to the set of format characters in the C standard
(but implemented separately in datetime objects, and therefore always
available). When used with the strptime() method, the %f directive
accepts from one to six digits and zero pads on the right.
I need result like 07:00 PM format. Is there any alternative method?
Thanks in advance.

If you're sure that the input will always be like that, you can just remove the extra digit before passing that string to strptime:
date_format = datetime.strptime('2017-01-02T19:00:07.9181202Z'[:-2] + 'Z', '%Y-%m-%dT%H:%M:%S.%fZ')
This is dirty, but gives the idea - remove the last two characters (the extra digit and "Z"), re-add the "Z".

Related

Convert date string to Datetime Python Format Error

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.

Python code for 'YYYY-MM-DD HH24:MI:SS.FF' format

I need python code for 'YYYY-MM-DD HH24:MI:SS.FF' format .
The result would be like this '2019-07-27 12:07:00.0'
sample code that I tried:
from datetime import datetime as dt
from datetime import timedelta
timestamp=(dt.now() - timedelta(1)).strftime('%Y-%m-%d %HH24:%MI:%SS.%FF')
Output:2019-09-05 10H24:31I:57S.2019-09-05F
Results should looks like 2019-09-05 10:31:57.0
Your format string just needs to be adapted - Python takes a single character to tell about the correct output - your repeated characters don't work like that.
Here is a corrected code example:
from datetime import datetime as dt
from datetime import timedelta
timestamp=(dt.now() - timedelta(1)).strftime('%Y-%m-%d %H:%M:%S.%f')
As you can see, I just removed some characters and wrote f in lowercase. The format characters that you chose already include padding and 24-hour format.
Example output: '2019-09-05 12:27:45.416157'
For a full list of format characters, please check the linked python documentation.
According to this documentation, you should use the following format:
timestamp=(dt.now() - timedelta(1)).strftime('%Y-%m-%d %H:%M:%S.%f')

Python: convert string to datetime object failed if the format is with comma

So i try to convert my string to date time object without knowing the format this way:
date = '019-03-13 17:35:35.855'
date_object = datetime.fromisoformat(date)
So this works fine but in case the datetime object failed if the format is with comma this fail:
date = '019-03-13 17:35:35,855'
date_object = datetime.fromisoformat(date)
ValueError: Invalid isoformat string: '2019-03-13 17:35:35,855'
And most of my files written with this comma format.
Any suggestions ?
datetime.fromisoformat expects you to send a string in a particular format:
Specifically, this function supports strings in the format(s) YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]], where * can match any single character.
If your format differs, you should use strptime and set your format in it. If you have no single format or you have dirty data, the only way you can to process it is to clean it first with some kind of data refining function.

What's the correct datetime format for the specified date string?

I have the following date string: 2019-05-12T14:52:13.136621898Z
For the life of me I can't figure out the format. The closest datetime format string that would make sense to me is:
%Y-%m-%dT%H:%M:%S.%fZ
I've searched StackOverflow, Google and the Python docs.
For such issues, it is worthy to look at datetime module and it's parser.parse function, which parses any datetime string without you needing to provide the format!
from dateutil import parser
dt_obj = parser.parse('2019-05-12T14:52:13.136621898Z')
print(dt_obj)
Also the closest format which fits your requirement is '%Y-%m-%dT%H:%M:%S.%fZ' which works with 2019-05-12T14:52:13.136621Z where '.%f` encapsulates microseconds with 6 decimal places but since your decimal has 9 decimal places, this won't work for you!
The format is called "round trip format" and Python does not have a format specifier for it, while e.g. .NET has the o format specifier. Therefore it might be a bit harder to craft a format string:
In your case it has 9 digits for sub-seconds. #Devesh said it should be 8 digits and the .NET framework uses 7 digits for it. But generally, %f should be ok.
You can't use a hard coded Z at the end, because that's the time zone. Z would only work for UTC, but would ignore all other time zones. The time zone format specifier is %z
As datetime says:
utcoffset() is transformed into a string of the form ±HHMM[SS[.ffffff]], where HH is a 2-digit string giving the number of UTC offset hours, MM is a 2-digit string giving the number of UTC offset minutes, SS is a 2-digit string giving the number of UTC offset seconds and ffffff is a 6-digit string giving the number of UTC offset microseconds. The ffffff part is omitted when the offset is a whole number of seconds and both the ffffff and the SS part is omitted when the offset is a whole number of minutes. For example, if utcoffset() returns timedelta(hours=-3, minutes=-30), %z is replaced with the string '-0330'
and
For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'

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