Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

time converting module

# input: 07:40:01PM
# output: 19:40:01

# First System:

def timeConversion(s):
    period = s[-2:]
    time = s[:-2]
    hour = int(time[:2])
    
    if period == 'PM' and hour < 12:                
        time = str(hour + 12) + time[2:]

    if period == 'AM' and hour == 12:
        time = '00' + time[2:]
        
    return time
s = input()
print(timeConversion(s))

# second code and use to module:

"""
from datetime import*
def timeConversion(s):
    return datetime.strftime(datetime.strptime(s, "%I:%M:%S%p"), "%H:%M:%S")
if __name__ == '__main__':
    s = input()
    print(timeConversion(s))
    
"""

# 3rd code and not used module.

'''

time = input()                  # time = input time.
sp = time.split(':')            # sp = split the ":"
if 'PM' in sp[2]:
    sp[0] = int(sp[0])+12
    sp[2] = sp[2].rstrip('PM')
elif 'AM' in sp[2]:
    sp[2] = sp[2].rstrip('AM')
    if sp[0] == '12':
        sp[0] = "0"
con_time = ''             # con_time = convert time.
for i in range(2):
    con_time += str(sp[i])+':'
con_time += sp[2]
print(con_time)
'''

Comment

PREVIOUS NEXT
Code Example
Python :: python how to restart thread 
Python :: .squeeze function in numpy 
Python :: tensorflow data augmentation 
Python :: cbind arrays python 
Python :: pandas filter 
Python :: telegram telethon get new user details 
Python :: comentar codigo en python 
Python :: python random numbers 
Python :: Dynamic Form Fields Django 
Python :: string slicing python 
Python :: python map 
Python :: interviewbit with Python questions solutions 
Python :: iterating string in python 
Python :: what is the django orm 
Python :: python how to create a class 
Python :: what is print in python 
Python :: install python anaconda 
Python :: multivaluedictkeyerror django 
Python :: 3d graph python 
Python :: python online 
Python :: return max(max(a,b),max(c,d)); 
Python :: append to a tuple 
Python :: python transpose 
Python :: pytest use fixture without running any tests 
Python :: queue class python 
Python :: /n in python 
Python :: how to change datatype of column in pandas 
Python :: how to add space in python 
Python :: telegram bot carousel 
Python :: python write to error stream 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =