How to get relevant month and date by inputting only the day of year in python
Question: I'm developing a phonebook application from python as a mini project in which
I'm having the requirement to store the NIC number of a person and then
display his/her gender, DOB and age. I have to derive these 3 information and
I'm able to derive the gender, but I don't know how to derive the DOB - only
the birth year because the NIC number's first 2 digits represent the year of
birth.

In a NIC number, the 3rd three digits are the day of year :- from 001 to 366.
I can seperate those 3 digits to another variable as well, but how do I derive
the month and the date of month which it refers to?

For example :

    
    
    derivedYear = 1996
    dayOfYear = 032
    
    finalDOB = "1996.02.01"
    print finalDOB
    

I want to know how to calculate the finalDOB value. I'm using python 2.7.6


Answer: You can use
[`timedelta()`](https://docs.python.org/2/library/datetime.html#datetime.timedelta).
It can take days as an argument, and when added to a date, will shift it by
that amount.

    
    
    import datetime
    
    year = 1996
    days = 32
    
    date = datetime.date(year, 1, 1) #Will give 1996-01-01
    delta = datetime.timedelta(days - 1) #str(delta) will be '31 days, 0:00:00'
    newdate = date + delta
    
    >>> str(newdate) 
    >>> 1996-02-01
    

or

    
    
    >>> newdate.strftime('%Y.%m.%d')
    >>> '1996.02.01'
    

