
In the following program, we will learn how to format date as dd/mm/YYYY HH:MM:SS. Python DateTime – Format Date & Time – dd/mm/YYYY HH:MM:SS We will learn much about these in the following example programs. These format codes if specified in the format, will be replaced with their corresponding values present in datetime.datetime object. Format Codesįollowing is the list of format codes that we use while formatting a string from datetime object using strftime() function. Strftime() function returns formatted datetime as string based on the given format. strftime(format)įormat is string that describes the specified format to strftime() function for creating date and time string from datetime.datetime object. Next: Write a Python program which accepts the radius of a circle from the user and compute the area.Following is the syntax of strftime() function. Previous: Write a Python program to get the Python version you are using. Have another way to solve this solution? Contribute your code (and comments) through Disqus. This code can be useful for logging or timestamping events in a program, or for displaying the current date and time in a user interface. %S: second as a zero-padded decimal number.%M: minute as a zero-padded decimal number.%H: hour (24-hour clock) as a zero-padded decimal number.%d: day of the month as a zero-padded decimal number.%m: month as a zero-padded decimal number.%Y: year with century as a decimal number.

The format codes used in this example are: The strftime() method returns a string representing the date, controlled by an explicit format string. The fourth line print (now.strftime("%Y-%m-%d %H:%M:%S")) uses the strftime() method of the datetime object to format the date and time as a string in the format "YYYY-MM-DD HH:MM:SS" and prints it to the console.The third line print ("Current date and time : ") prints the string "Current date and time : " to the console.

