Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python datetime into 12-hour format

d.strftime("%I:%M")
Comment

#9. Python program to convert time from 12 hour to 24 hour format

# Python program to convert time
# from 12 hour to 24 hour format
 
# Function to convert the date format
def convert24(str1):
     
    # Checking if last two elements of time
    # is AM and first two elements are 12
    if str1[-2:] == "AM" and str1[:2] == "12":
        return "00" + str1[2:-2]
         
    # remove the AM    
    elif str1[-2:] == "AM":
        return str1[:-2]
     
    # Checking if last two elements of time
    # is PM and first two elements are 12
    elif str1[-2:] == "PM" and str1[:2] == "12":
        return str1[:-2]
         
    else:
         
        # add 12 to hours and remove PM
        return str(int(str1[:2]) + 12) + str1[2:8]
 
# Driver Code        
print(convert24("08:05:45 PM"))
Comment

PREVIOUS NEXT
Code Example
Python :: how to reverse array in ruby 
Python :: get local python api image url 
Python :: bar plot fix lenthgy labels matplot 
Python :: freq count in python 
Python :: how to get iheight in pyqt5 
Python :: python read music stream 
Python :: playsound moudle python 
Python :: command prompt pause in python 
Python :: python list subdirectories 
Python :: python square root 
Python :: import a txt file into python 
Python :: openpyxl get last non empty row 
Python :: import random py 
Python :: python snake game 
Python :: how to replace nan values with 0 in pandas 
Python :: Python - Drop row if two columns are NaN 
Python :: multiply column of dataframe by number 
Python :: openpyxl delete column by name 
Python :: delete index in elasticsearch python 
Python :: python check folder exist 
Python :: Dummy or One Hot Encoding code with pandas 
Python :: from django.conf.urls import patterns 
Python :: python write 
Python :: python element wise multiplication list 
Python :: pandas repeat rows n times 
Python :: Setting a conditional variable in python. Using an if else statement in python. 
Python :: pickle.load python 
Python :: how to sort a list in python using lambda 
Python :: grab a href using beuatiful soup 
Python :: How to Add R to Jupyter Notebook 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =