1. Create a variable today that contains today's date.
import datetime
today = datetime.date.today()
  1. Print the date and time in the following format: yyyy-mm-dd hh:mm:ss.
import datetime
today = datetime.date.today()
print(today.strftime('%Y-%m-%d %H:%M:%S'))
  1. Calculate the number of days until your next birthday.
import datetime
today = datetime.date.today()
birthday = datetime.date(year,month,day)
next_birthday = datetime.date(today.year, birthday.month, birthday.day)
if next_birthday<today:
    next_birthday = datetime.date(today.year + 1, birthday.month, birthday.day)
days_until_birthday = (next_birthday - today).days
print(days_until_birthday)

  1. Create a variable **time_zone**that contains your time zone as a timezone object.
import pytz
from datetime import datetime

# create timezone object for India
IST = pytz.timezone('Asia/Kolkata')

# get current time in India
current_time = datetime.now(IST)

# print current time in India
print(current_time)
  1. Write a program to display the next 5 days from today in a list.
import datetime as dt
today = dt.datetime.today()
u = []
for x in range(1,6):
    n= today+dt.timedelta(days=x)
    p = n.strftime("%A")
    u.append(p)
print(u)
  1. Write a Python function **get_day_of_week(date_string, date_format)**that takes in a date string in **date_format**and returns the day of the week as a string.
from datetime import datetime

def get_day_of_week(date_string, date_format):
    try:
        # Convert date string to datetime object using strptime()
        date_obj = datetime.strptime(date_string, date_format)
    except ValueError:
        # Return None if date string is not in date_format
        return None
    # Get day of week as string using strftime() with %A format code
    return date_obj.strftime("%A")
#get_day_of_week("2022-01-15", "%Y-%m-%d")
#output="Saturday”
  1. Write a Python function parse_date_string(date_string) that takes in a date string in one of two formats:

and returns a tuple **(year, month, day)**representing the year, month, and day of the date.

from datetime import datetime

def parse_date_string(date_string):
    try:
        # Attempt to parse date string in YYYY-MM-DD format
        date_obj = datetime.strptime(date_string, "%Y-%m-%d")
    except ValueError:
        try:
            # Attempt to parse date string in DD-Mon-YYYY format
            date_obj = datetime.strptime(date_string, "%d-%b-%Y")
        except ValueError:
            # Raise ValueError if date string is not in either format
            raise ValueError("Invalid date string format")
    # Return tuple of year, month, and day
    return date_obj.year, date_obj.month, date_obj.day
#parse_date_string("2022-03-12")
#(2022, 3, 12)
#parse_date_string("12-Mar-2022")
#(2022, 3, 12)

8. What is the difference between strptime() and strftime() functions in Python?

Ans. The strptime() function is used to parse a string representing a datetime and convert it into a datetime object, while the strftime() function is used to format a datetime object into a string. In other words, strptime() is used to parse a string into a datetime object, while strftime() is used to convert a datetime object into a string.

9. Can you use strptime() to parse a string representing a time zone? If not, what other functions or libraries can you use for this purpose?