Data and Time Format in Python

Working with Date and Time Format in Python programming to get Computer's current date and time in different formats by using a standard Date and Time 'datetime' library of Python programming language.

Program Printing Current Day

Program

import datetime
print(datetime.date.today().strftime("%d")) #printing Current Day.
Output
06

Program Printing Current Date

Program

import datetime
print(datetime.date.today().strftime("%d/%m/%Y"))
Output
06/10/2023

Program Printing Current Date Another Method

Program

import datetime
print(datetime.date.today().strftime("%A, %d %B,%Y"))
Output
Friday, 06 October,2023

Program User Given Date Using as a Computer Date
Program

import datetime
mydate = datetime.date(1943, 3, 13) #assigning year, month, day to
print(mydate.strftime("%d/%B/%Y")) #'mydate' and printing.
Output
13/March/1943

Program Printing Current Date and Time etc
Program

import time
import datetime
print("Time in seconds since the epoch: %s" %time.time())
print("Current date and time: ", datetime.datetime.now())
print("Or like this: ", datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))
print("Current year: ", datetime.date.today().strftime("%Y"))
print("Month of year: ", datetime.date.today().strftime("%B"))
print("Week number of the year: ", datetime.date.today().strftime("%W"))
print("Weekday of the week: ", datetime.date.today().strftime("%w"))
print("Day of year: ", datetime.date.today().strftime("%j"))
print("Day of the month : ", datetime.date.today().strftime("%d"))
print("Day of week: ", datetime.date.today().strftime("%A"))
Output
Time in seconds since the epoch: 1696589688.1330054
Current date and time: 2023-10-06 15:54:48.133005
Or like this: 23-10-06-15-54
Current year: 2023
Month of year: October
Week number of the year: 40
Weekday of the week: 5
Day of year: 279
Day of the month : 06
Day of week: Friday