You are viewing a single comment's thread. Return to all comments →
A very simple one-liner in PHP:
echo (new DateTime(fgets(fopen("php://stdin", "r"))))->format("H:i:s");
that's a great addition to these comments!
Less bytes )))
Almost a one-liner in Python :)
from time import strptime, strftime
print strftime("%H:%M:%S", strptime(raw_input(), "%I:%M:%S%p"))
time = raw_input()
dtdate = datetime.datetime.strptime(time,'%I:%M:%S%p')
this is what i wrote but it says wrong error can you help me out.
fixed it, some minor problems
time = input()
dtdate = datetime.datetime.strptime(time, '%I:%M:%S%p')
Use triple backticks (`) to enclose your code, it will be easier to help you.
Could you explain this? I tried this code in my python 2.7, but it told me that there's no modules with this name.
Here's my solution
time = raw_input()
night = 0
if 'PM' in time:
night = 12
time = time[:8].split(':')
if time == '12':
time = 0
time = str(int(time) + night)
newtime = str()
for x in time:
newtime += x + ':'
if len(newtime) < 9:
newtime = '0' + newtime
@SanshiNron Are we suppose to use this? is this for checking our logic skills with the language or for using the goodness of language?
can u explain the code ,i am new in php i cnt understand the syntax and logic.plzz explain sir
While using a built-in time-formating function may solve the challenge, it doesn't do much for our algorithm skills. Here's a PHP solution that does it the "long way".
// get input
raw_time = trim(fgets(hour = substr(time_period = substr(final_time = substr(time_period == 'AM' && hour = '00';
if (Misplaced &hour == '00') time_period == 'PM' && hour = (final_time = final_time,2,6);
echo $final_time . "\n";
changing your if-else statement to this saves you another line:
if (Misplaced &hour != 12) final_time = final_time,2,6);
A little simplier though NOT the OOP way of doing it
echo date('H:i:s',strtotime(fgets(fopen ("php://stdin","r"))));