#๐ Is there a way to convert time from 12 hours to 24 hours format in a single expression?
11 messages ยท Page 1 of 1 (latest)
@ebon igloo
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
It's always possible to do something like that, but the result will likely be ugly and less readable.
I actually created this as a golfing challenge like 7 years ago: https://codegolf.stackexchange.com/questions/105858/12-hour-to-24-hour-time-converter
There's at least one Python answer.
thought so, it isn't possible to do one that covers all of the edge cases
example inputs would be helpful
>>> datetime.strptime(input(), "%I %p").strftime("%H")
11 AM
'11'
>>> datetime.strptime(input(), "%I %p").strftime("%H")
5 PM
'17'
can't use libraries, the input is the form "XX PM" or "XX AM"
did this and it worked time_in_hrs = int(time[:2]) % 12 + (12 if 'PM' in time else 0)
That's very close-looking to the existing Python 2 answer:
lambda s:`int(s[:2])%12+12*("p"in s)`+s[3:5]
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.