0% found this document useful (0 votes)
19 views

Python

The document contains Python code examples that demonstrate working with dates and times. The examples show how to: print the current date and time; get the Python version; display various date time formats like year, month, day etc.; print yesterday, today and tomorrow's date; and print the dates for the next 5 days starting from today.

Uploaded by

my stories
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Python

The document contains Python code examples that demonstrate working with dates and times. The examples show how to: print the current date and time; get the Python version; display various date time formats like year, month, day etc.; print yesterday, today and tomorrow's date; and print the dates for the next 5 days starting from today.

Uploaded by

my stories
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Print current time and date

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

Write a Python program to get the Python version you are using.

import sys
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)

Write a Python script to display the various Date Time formats.

a) Current date and time


b) Current year
c) Month of year
d) Week number of the year
e) Weekday of the week
f) Day of year
g) Day of the month
h) Day of week

import time
import datetime
print("Current date and time: " , datetime.datetime.now())
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"))

Write a Python program to print yesterday, today, tomorrow.

import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 1)
tomorrow = today + datetime.timedelta(days = 1)
print('Yesterday : ',yesterday)
print('Today : ',today)
print('Tomorrow : ',tomorrow)

Write a Python program to print the next 5 days starting today

import datetime
base = datetime.datetime.today()
for x in range(0, 5):
print(base + datetime.timedelta(days=x))

You might also like