Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

datetime conversion

# 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 :: time converting module 
Python :: convert string to datetime python 
Python :: List Get a Element 
Python :: replace by positions a string in a list with another string python 
Python :: python - input: integer 
Python :: python print every row of dataframe 
Python :: child class in python 
Python :: what is serializer in django 
Python :: run ipython inside pipenv 
Python :: iterate through dict with condition 
Python :: Python list append tutorial 
Python :: python with quick sort 
Python :: percentage plot of categorical variable in python woth hue 
Python :: regex python 
Python :: pandas how to read csv 
Python :: python variables and data types 
Python :: @ in python 
Python :: merge sorting in python 
Python :: download gzip file python 
Python :: how to return the sum of two numbers python 
Python :: np where and 
Python :: how to delete whole list in python 
Python :: matrix multiplication python without numpy 
Python :: python print an array 
Python :: how to make a variable in python 
Python :: numpy array into tuple 
Python :: ImportError: No module named pandas 
Python :: row count pandas 
Python :: get center position of countries geopandas 
Python :: Patch loop runner _run_once 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =