strptime

ValueError: time data '24:00' does not match format '%H:%M'

ValueError: time data '24:00' does not match format '%H:%M' Question: I’m having serious trouble converting 24 hour time to 12 hour. def standard_time(t): t = datetime.strptime(t, “%H:%M”) return t When fed in ’24:00′ we get ValueError: time data ’24:00′ does not match format ‘%H:%M’ I also attempt converting using %I (12 hour) instead of %H, …

Total answers: 1

How to format date string via multiple formats in python

How to format date string via multiple formats in python Question: I have three date formats: YYYY-MM-DD, DD.MM.YYYY, DD/MM/YYYY. Is it possible to validate and parse strings such as 2014-05-18 or 18.5.2014 or 18/05/2019? Asked By: Alexey Sh. || Source Answers: Try each format and see if it works: from datetime import datetime def try_parsing_date(text): …

Total answers: 5

Get date from week number

Get date from week number Question: Please what’s wrong with my code: import datetime d = “2013-W26” r = datetime.datetime.strptime(d, “%Y-W%W”) print(r) Display “2013-01-01 00:00:00”, Thanks. Asked By: Ali SAID OMAR || Source Answers: A week number is not enough to generate a date; you need a day of the week as well. Add a …

Total answers: 8

How to initialize a datetime.time object from a string?

How to initialize a datetime.time object from a string? Question: Is there a method like datetime.datetime.strptime(), that accepts a string like ’16:00′ and returns a datetime.time(16,0) object (i.e., an object that holds only time, not date)? Edit: I could use datetime.datetime.strptime(), but it would return a datetime.datetime, and I want only time, not a date. …

Total answers: 3

how to initialize time() object in python

how to initialize time() object in python Question: I am trying to initialize a time object like this: t = datetime.time(0,0,0) but I am getting this error: descriptor ‘time’ requires a ‘datetime.datetime’ object but received a ‘int’ I have these things imported import datetime from datetime import datetime, date, time import time They seem a …

Total answers: 3

Parsing datetime strings containing nanoseconds

Parsing datetime strings containing nanoseconds Question: I have some log files with times in the format HH:MM::SS.nano_seconds (e.g. 01:02:03.123456789). I would like to create a datetime in python so I can neatly do math on the time (e.g. take time differences). strptime works well for microseconds using %f. Do the Python datetime and time modules …

Total answers: 5

How to convert integer into date object python?

How to convert integer into date object python? Question: I am creating a module in python, in which I am receiving the date in integer format like 20120213, which signifies the 13th of Feb, 2012. Now, I want to convert this integer formatted date into a python date object. Also, if there is any means …

Total answers: 5

How to remove unconverted data from a Python datetime object

How to remove unconverted data from a Python datetime object Question: I have a database of mostly correct datetimes but a few are broke like so: Sat Dec 22 12:34:08 PST 20102015 Without the invalid year, this was working for me: end_date = soup(‘tr’)[4].contents[1].renderContents() end_date = time.strptime(end_date,”%a %b %d %H:%M:%S %Z %Y”) end_date = datetime.fromtimestamp(time.mktime(end_date)) …

Total answers: 5

How can I account for period (AM/PM) using strftime?

How can I account for period (AM/PM) using strftime? Question: Specifically I have code that simplifies to this: from datetime import datetime date_string = ‘2009-11-29 03:17 PM’ format = ‘%Y-%m-%d %H:%M %p’ my_date = datetime.strptime(date_string, format) # This prints ‘2009-11-29 03:17 AM’ print my_date.strftime(format) What gives? Does Python just ignore the period specifier when parsing …

Total answers: 5