Open In App
Related Articles

Python program to calculate Date, Month and Year from Seconds

Improve Article
Save Article
Like Article

Given the number of seconds, the task is to write a Python program to calculate the date, month, and year in the format MM-DD-YYYY that have been passed from 1 January 1947.


Input: 0
Output: 01-01-1947

Input: 123456789
Output: 11-29-1950

Input: 9876543210
Output: 12-22-2259

Approach to Calculate Date, Month, and Year from Seconds

Here is the step-by-step approach on how to calculate the date, Month, and Year in Seconds.

Create a function to Get the Number of Days in a Year

When creating a function to calculate the number of days in a year, it is important to take into account whether the year is a leap year or a common (non-leap) year. Leap years have an extra day in the month of February, making them longer than common years. Specifically, February in a leap year has 29 days instead of the usual 28.


# function to get number of
# days in the year
# if leap year then 366
# else 365
def dayInYear(year):
    if (year % 4) == 0:
        if (year % 100) == 0:
            if (year % 400) == 0:
                return 366
                return 365
            return 366
        return 365

Create a Function to Count the Years after 1947

To create a function that counts the number of years after 1947, we need to take a list of years as input and then count the years that are greater than 1947.


# counting the years after 1947
def getYear(days):
    year = 1946
    while True:
        year += 1
        dcnt = dayInYear(year)
        if days >= dcnt:
            days -= dcnt
    return year, days

Create a Function to Count the Number of Months

To create a function that counts the number of months in a given list, we first need to understand the criteria for identifying months. In the context of a date, a month is represented as an integer value between 1 and 12.


# counting the number of months
def monthCnt(days, year):
    if days == 0:
        return 1, 0
        month_num = 1
        months = [31, 28, 31, 30, 31,
                  30, 31, 31, 30, 31,
                  30, 31]
        if dayInYear(year) == 366:
            months[1] = 29
        for day in months:
            if day < days:
                month_num += 1
                days -= day
        return month_num, days

Create a Function to Get a Date using the Number of Seconds

In order to generate a date using the number of seconds, it is important to understand the idea of epoch time or Unix time. Python module has all the essential tools required to work with dates and times.


# getting date using number of seconds
def getDate(num_sec):
    # converting seconds into days
    days_sec = 24*60*60
    days = num_sec//days_sec
    day_started = False
    # if some seconds are more
    if days % days_sec != 0
        day_started = True
    # getting year   
    year, days = getYear(days) 
     # getting month
    month, days = monthCnt(days, year)
    if day_started or num_sec == 0:
        days += 1
    # preparing date_format
    date = ""
    if month < 10:
        date = date+"0"+str(month)
        date = date+str(month)
    date = date+"-"
    if days < 10:
        date = date+"0"+str(days)
        date = date+str(days)
    date = date+"-"
    date = date+str(year)
    return date

Create the driver code and call the Required Function

To create the driver code and call the required function.


# Driver Code
# returns 01-01-1970
date_format = getDate(0)
# returns 11-29-1973
date_format = getDate(123456789)
# returns 12-22-2282
date_format = getDate(9876543210)

Below is the complete program based on the above stepwise approach

In order to showcase the entire program, we will follow the step-by-step process explained earlier to generate a Python script that computes the area of a rectangle based on its length and width. This involves defining the necessary function, creating the driver code, and invoking the function to obtain the area.


from datetime import datetime, timedelta
def add_seconds_to_date(date_str, seconds):
    date_format = "%d/%m/%Y"
    date = datetime.strptime(date_str, date_format)
    updated_date = date + timedelta(seconds=seconds)
    updated_date_str = updated_date.strftime("%Y/%m/%d")
    return updated_date_str
date_str = "1/1/1947"
seconds = 123456789
result = add_seconds_to_date(date_str, seconds)



The time complexity of this program is O(y), where y is the number of years between 1947 and the input year, because the getYear() function iterates over each year from 1947 to the input year.

The space complexity of this program is O(1), because the amount of memory used by the program is constant and does not depend on the input size. The program does not use any data structures that grow with the input size.

Last Updated : 28 Jul, 2023
Like Article
Save Article
Similar Reads
Related Tutorials