Search Here

Python strftime Datetime Formatting

We have seen how to use python to retrieve basic date and datetime information. Lets take a look how to the format the output using a set of predefined string control code. We can achieve this using python strftime function.
Python strftime Datetime Formatting


In this example we have already set import statement "datetime" to import the python datetime class so you don’t have to do that because we have already done in Python datetime article.


How to use python strftime function


#!/usr/bin/python3
from datetime import datetime

def main():
    # Times and dates can be formatted using a set of predefined string    
    now = datetime.now() # get the current date and time
    
    # Date Formatting Controll Codes
    # %y/%Y - Year, %a/%A - weekday, %b/B% - month, %d - day of month
    
    print(now.strftime("%Y"))  
    
if __name__ == "__main__": main()        
 



In above code I am retrieving the current date and time using the "datetime" class function. Now that we have get the current date and time, let's take a look some of the option that I have available to format the datetime output. So to format date you can use control code. Here %y/%Y upper and lower mean you can format the date abbreviated of full.

# %y/%Y - Year, %a/%A - weekday, %b/B% - month, %d - day of month      
 


  • %y - 18
  • %Y - 2018
If you familiar with the other programming languages like C and C++, they use the standard C library to control dates formatting. These control code might familiar to you. So lets see how to actually use them.
Here I am using strftime. The strftime function on the value that I got back for the current date and time takes a string which is a control string ("%Y") and replaces whatever the control code is. Remember here control code mean (# %y/%Y - Year, %a/%A - weekday, %b/B% - month, %d - day of month). In this case the control code is %Y, the value of current year. So this will print the full year with century (2018). You can see its print out 2018.

And if we use the control code with lower %y, than this will print the abbreviated year only (18).


Python strftime Datetime Formatting

Now let's move to more advance.

#!/usr/bin/python3
from datetime import datetime

def main():
    # Times and dates can be formatted using a set of predefined string    
    now = datetime.now() # get the current date and time
    
    # Date Formatting Controll Codes
    # %y/%Y - Year, %a/%A - weekday, %b/B% - month, %d - day of month
    
    print("Full Date: ",now.strftime("%Y"))
    print("Abbreviated Date: ",now.strftime("%y"))
    print(now.strftime("%a, %d %B, %y"))  # will print out Sat, 17 February, 18
    
if __name__ == "__main__": main()      
 



In above code I am printing the abbreviated day name, followed by day number, followed by the full month name and the abbreviated year. So this is going to print out something like (Sat, 17 February, 18)
 Python strftime Datetime Formatting


Here its print out "Sat" because the lower case %a, so its abbreviated, if I change this to upper case %A than it will print out full name "Saturday".

Locale specific information using python strftime

Python also give you a way of printing out the locale specific information. So its benefit is that rather than having a figure out your application is being run and how to print out information using the current localized version of date and datetime. There is the control code that it does for you.
 
#!/usr/bin/python3
from datetime import datetime

def main():
    # Times and dates can be formatted using a set of predefined string    
    now = datetime.now() # get the current date and time
    
    # Date Formatting Controll Codes
    # %y/%Y - Year, %a/%A - weekday, %b/B% - month, %d - day of month
    
    print("Full Date: ",now.strftime("%Y"))
    print("Abbreviated Date: ",now.strftime("%y"))
    
    print(now.strftime("%a, %d %B, %y"))  # will print out Sat, 17 February, 18
    
    # %c - locale's date and time, %x - locale's date, %X - locale's time
    print(now.strftime("%c"))
    print(now.strftime("%x"))
    print(now.strftime("%X"))
    
if __name__ == "__main__": main()      
       
 



So %c is the locale date and time, lower case %x is just a date and upper case %X is just the time.

Python strftime Datetime Formatting

You can see that it's printing out date and time information, the date and time all appropriate for how I have my locale setup on my machine (Computer). If I was running a computer which is in Europe the information you have seen is probably different, because it depend upon the locale setting they are.

Time Formatting using python strftime


Let's take a look at time formatting now. Just the same way you can print formatted date information you can print formatted time information as well.


#!/usr/bin/python3
from datetime import datetime

def main():
    # Times and dates can be formatted using a set of predefined string    
    now = datetime.now() # get the current date and time
    
    # Date Formatting Controll Codes
    # %y/%Y - Year, %a/%A - weekday, %b/B% - month, %d - day of month
    
    print("Full Date: ",now.strftime("%Y"))
    print("Abbreviated Date: ",now.strftime("%y"))
    
    print(now.strftime("%a, %d %B, %y"))  # will print out Sat, 17 February, 18
    
    # %c - locale's date and time, %x - locale's date, %X - locale's time
    print(now.strftime("%c"))
    print(now.strftime("%x"))
    print(now.strftime("%X"))
    
    # Formatting Time
    # %I%H - 12/24 Hour, %M - minute, %S - second, %p - locale's AM/PM
    print(now.strftime("%I:%M:%S:%p")) # 12-Hour:Minute:Second:AM
    print(now.strftime("%H:%M")) #24-Hour:Minute
    
if __name__ == "__main__": main()      
       
 



So same kind of idea you got %I:%H for 12 vs 24 Hours, %M for minutes, %S for seconds and %p for locale's AM vs PM. So if I want to print out 12 hours base time with minute, second and AM/PM then I will use.

       
print(now.strftime("%I:%M:%S:%p")) # 12-Hour:Minute:Second:AM    
 



If I want to use 24 hours time pattern than I will use.


       
print(now.strftime("%H:%M")) #24-Hour:Minute   
 



As you can see python provide some rich and pretty controls for printing out dates and time using complex formatting just using some controls code.

Post a Comment

1 Comments