Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Converting 12 hour clock time to 24 hour clock time

# 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

convert 12 hour into 24 hour time

>>> from datetime import datetime

    >>> m2 = '1:35 PM'
    >>> in_time = datetime.strptime(m2, "%I:%M %p") #Converts string to 12 hour time format
    >>> out_time = datetime.strftime(in_time, "%H:%M") #Converts 12 hour to 24 hour time format
    >>> print(out_time)
    13:35
Comment

Converting 12 hour clock time to 24 hour clock time

import datetime

x = datetime.datetime.now()
print("Now Time = ",x.strftime("%H:%M:%S %p"))
print(x.strftime("Date = %d"))
print(x.strftime("Weekday = %A"))
print(x.strftime("Month = %B"))
print(x.strftime("Year = %Y"))


# There is more to know-

print("Time = ",x.strftime("%H:%M:%S %p"))
print("Full name Year  = ",x.year)
print(x.strftime("Full name Month  = %B"))
print(x.strftime("Full name Day = %A"))
print(x.strftime("Date of Month(01-31) = %d"))
print(x.strftime("Weekday, short version = %a"))
print(x.strftime("Weekday as a number(0-6) = %w"))
print(x.strftime("Month name short = %b"))
print(x.strftime("Month as number = %m"))
print(x.strftime("Short name Year = %y"))
print(x.strftime("Full Year name = %Y"))
print(x.strftime("Time Zone = %Z"))
print(x.strftime("Local Date = %x"))
print(x.strftime("Lacal Date time = %X"))
print(x.strftime("Local date and time = %c"))

# All Varsion Datetime System:
#A reference of all the legal format codes:
'''
Directive:	                                   Description:	                                                       Example:     

%a         ---------                    Weekday, short version                ---------                =   Wed

%A        ---------                     Weekday, full version                  ---------           = Wednesday

%w        ---------           Weekday as a number 0-6, 0 is Sunday      -------           =      3

%d        ---------                     Day of month 01-31                 ---------                       =    31

%b           ---------                  Month name, short version	       ---------                  =   Dec

%B           ---------                  Month name, full version	        ---------             = December

%m        ---------                 Month as a number 01-12        ---------                     =     12

%y        ---------                Year, short version, without century        ---------        = 18

%Y        ---------                    Year, full version           ---------                                =  2018

%H        ---------                     Hour 00-23                      ---------                           =    17

%I        ---------                     Hour 00-12	        ---------                                 =   05

%p        ---------                     AM/PM	        ---------                              =     PM

%M        ---------                     Minute 00-59	        ---------                               =     41

%S        ---------                     Second 00-59	        ---------                                 =   08

%f        ---------                     Microsecond 000000-999999        ---------        =	548513

%z        ---------                     UTC offset        ---------                                      =      +0100

%Z        ---------                     Timezone	        ---------                             =      CST

%j        ---------                     Day number of year 001-366            ---------        =  365

%U   ---------  Week number of year, Sunday as the first day of week, 00-53--------- = 52

%W   --------- Week number of year, Monday as the first day of week, 00-53 --------  = 52

%c        ---------      Local version of date and time        ---------             =  Mon Dec 31 17:41:00 2018	

%x        ---------                     Local version of date	        ---------              =  12/31/18	

%X        ---------                     Local version of time	        ---------              =  17:41:00	

%%        ---------                     A % character	                       ---------                 =    %	

%G        ---------                     ISO 8601 year                          ---------                    =      2018	

%u        ---------                     ISO 8601 weekday (1-7)            ---------                 =   1	

%V           ---------             ISO 8601 weeknumber (01-53)	        ---------              = 01

'''
Comment

PREVIOUS NEXT
Code Example
Python :: python modulo 
Python :: Removing Elements from Python Dictionary Using pop() method 
Python :: Python NumPy delete Function Example Deletion from 1D array 
Python :: django action when create model 
Python :: convert sentence to list of words python 
Python :: how to limit a command to a role in discord.py 
Python :: python alphanum 
Python :: .corr python 
Python :: pathy python 
Python :: if loop python 
Python :: Python list function tutorial 
Python :: pandas df number of columns 
Python :: python import statement 
Python :: Set .difference() Operation in python3 
Python :: drop columns pandas dataframe 
Python :: opencv resize image 
Python :: if or python 
Python :: python 2 
Python :: Code example of Python Modulo Operator 
Python :: why is c faster than python 
Python :: python create list 
Python :: how to convert a list to a string in python 
Python :: for _ in range() in python 
Python :: invalid literal for int() with base 10 in python 
Python :: python exit if statement 
Python :: how to make a label in python 
Python :: pythonanywhere django 
Python :: login system in django 
Python :: bar break matplotlib 
Python :: python pprint on file 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =