python list

Python| Current Date and Time in different formats.

In this tutorial, We will learn how to convert the current day and timestamp in different formats. To manipulate the current date and time we need to import the standard datetime python module which comes along with python by default so we don’t need to install manually. We can simply use it by importing the datetime module.

from datetime import datetime

Strftime.

Strftime method returns the date and time generated using date, time and datetime object in different formats.

The program below converts current timestamp into different formats.

Source Code.

from datetime import datetime
timestamp=datetime.now()
print("Current Timestamp :",timestamp)
#print current timestamp in yyyy-mm-dd, HH:MM:SS
cur_day_format = timestamp.strftime("%Y-%m-%d,%H:%M:%S ")
print("Current Timestamp (YYYY-MM-DD, HH:MM:SS):", cur_day_format)
#current day in yyyymmdd format
cur_day_format = timestamp.strftime("%Y%m%d")
print("Current Date(YYYYMMDD):", cur_day_format)
#current day in dd-mm-yyyy format
cur_day_format=timestamp.strftime("%d-%m-%Y")
print("Current Date(DD-MM-YYYY):",cur_day_format)
#current day in dd/mm/yyyy format
cur_day_format=timestamp.strftime("%d/%m/%Y")
print("Current Date(DD-MM-YYYY):",cur_day_format)
#Current day in textual month day and Year(Month dd, YYYY)
cur_day_format = timestamp.strftime("%B %d, %Y")
print("Current Date(Month DD,YYYY):", cur_day_format)
#Current day in textual day and DD textual Month , YYYY
cur_day_format =timestamp.strftime("%A %d %B, %Y")
print("Current Date(Day  DD Month, YYYY):", cur_day_format) 

 

Output: