UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

A python program using the datetime module

Ashish K Sharma
11/11/2022 0 0

Task : To write a program in Python to find out, in any given year, Friday the 13th dates i.e 13th day of a month which was a Friday.

 

                                                                  SOLUTION

To perform this task, we need to first understand how Python handles dates and times. It does so by a built-in module called datetime. datetime helps us in various ways like:

                   A) It helps us to identify and process time-related elements like dates, hours, minutes,                                 seconds, days of the week, months, years, etc.

                   B) It offers various services like managing time zones and daylight savings time.

                    C) It can work with timestamp data.

                     D) Also,it can extract the day of the week, day of the month, and other date and time                                  formats from strings.

 

There are five main object classes associated with this module viz 

  • datetime – It enables us to manipulate times and dates together (month, day, year, hour,         second, microsecond).
  • date – Enables us to manipulate dates independent of time (month, day, year).
  • time – Enables us to manipulate time independent of date (hour, minute, second, microsecond).
  • timedelta— A duration of time used for manipulating dates and measuring.
  • tzinfo— An abstract class for dealing with time zones.

Depending on the type of task we wish to perform, we make use of these classes as and when required.

Thus,to handle anything related to date and time in Python,datetime is our module of choice.

Now, we will see how to use it in programs!

 

 

 Now, in order to perform the given task, we need,from any date,the following item:

  1. weekday using a function of the datetime class.

And, given that we already have the year and month information, we have to just combine these pieces of information to find out what we seek. But, first, in order to start using the date time object, we need to import its datetime class. So, now, we will look at the program to see how exactly  the task is performed:

 

from datetime import datetime    ## import datetime class from datetime module

monthlist = ['Jan','Feb','Mar','Apr','May','Jun','July','Aug','Sept','Oct','Nov','Dec']  ##List of months

year = input("Enter Year-->")   ## Year is inputted

for i in range(len(monthlist)):  ## This loop provides the month and the days of that month

        mah = monthlist[i]

    if (monthlist[i] == 'Jan'):

        mnthnum = 1

        daynum = 31

 

    if (monthlist[i] == 'Feb'):

        mnthnum = 2

        daynum = 28

 

    if (monthlist[i] == 'Mar'):

        mnthnum = 3

        daynum = 31

 

    if (monthlist[i] == 'Apr'):

        mnthnum = 4

        daynum = 30

 

    if (monthlist[i] == 'May'):

        mnthnum = 5

        daynum = 31

 

    if (monthlist[i] == 'Jun'):

        mnthnum = 6

        daynum = 30

 

    if (monthlist[i] == 'July'):

        mnthnum = 7

        daynum = 31

 

    if (monthlist[i] == 'Aug'):

        mnthnum = 8

        daynum = 31

 

    if (monthlist[i] == 'Sept'):

        mnthnum = 9

        daynum = 30

 

    if (monthlist[i] == 'Oct'):

        mnthnum = 10

        daynum = 31

 

    if (monthlist[i] == 'Nov'):

        mnthnum = 11

        daynum = 30

 

    if (monthlist[i] == 'Dec'):

        mnthnum = 12

        daynum = 31   

       

 

    for j in range(daynum):  ## This loop provides each and every day of a given month

          ## Create a date string from the year, month and day information

         dstr = str(year)+"-"+str(mnthnum)+"-"+str(j+1)  

    

        

         ##strptime function converts a date string to a date object. 

         ## strptime() takes two arguments: the date string(dstr) and the string "%Y-%m-%d"

         ## , an another string that tells strptime() how to interpret the input string dstr.

         my_date = datetime. strptime(dstr,"%Y-%m-%d")

         ##The weekday() function extracts the day of the week from the date

         wkday = my_date.weekday()

         ## Check for Friday,the 13th date in the given month

         if (wkday == 4) and ((j+1) == 13):

            print "Friday, the13th falls on”, dstr

    

 

 

                                                                  OUTPUT:

                                                                Enter Year-->2010

                                                                Friday, the13th falls on 2010-8-13

                                                                >>>

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Build an interactive Dictionary using Python 3.x
I have downloaded a 5MB data base consisting of words and their meanings in a json file . I want to write a python progrsm which will ask the user to enter a word and then the program will search through...
B

Biswanath Banerjee

0 0
0

Be prepared to get trained--init
Before starting the training,students must be mentally prepared for acceptance of new knowledge. Students must attend training with open minded forgetting the position they are working.This will help...
S

Smartnub Softsolutions

0 0
0

Python Important Operation Formats and Sytnax.
Hello Python Enthusiast, Though Python is considered as one of the easisest language to learn in the market, many freshers and beginners will find great difficulty in understansing the syntax and also...

Machine Learning With Python
1. Course description: Machine Learning with Python has been designed for the provision of having strong hold in creating Machine learning algorithms with the base of Python. This has been preferred as...
J

Python Dictionary
Python dictionary is an unordered collection of items. While other compound datatypes have only value as an element, a dictionary has a key: value pair. Dictionaries are optimized to retrieve values when...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more