Python program to calculate Date, Month and Year from Seconds


Generally, time is given in hours or minutes or seconds and from the given seconds, we can calculate the number of days, months and years. There are different modules and functions available in python such as datetime, time and divmod() which helps us to calculate the date month and year from seconds.

Using Datatime module

Datetime module offers classes to manipulate the dates and times. This module provides various functions and methods such as date, time, datetime, timedelta, minyear, maxyear, UTC etc.

In the datetime method of datetime module we have the function utcfromtimestamp() which takes the seconds as the input argument and converts the seconds into year, date and month.

Syntax

The syntax for using the utcfromtimestamp() function is as follows.

datetime.datetime.utcfromtimestamp(seconds)

Example

In this example we are passing the number of seconds as 1706472809 to the utcfromtimestamp() of the datetime module then returns the year as 2024, date as 28 and month as 1.

import datetime
def calculate_date(seconds):
   date = datetime.datetime.utcfromtimestamp(seconds)
   year = date.year
   month = date.month
   day = date.day
   return day, month, year
seconds = 1706472809
day, month, year = calculate_date(seconds)
print("The day, month and year from the given seconds:",day, month, year)

Output

The day, month and year from the given seconds: 28 1 2024

Using time module

The time module provides various time related functions such as asctime, cloc_gettime and gmtime etc.

The gmtime() function takes the seconds as the input argument and converts the seconds to a tuple and extract the year, day and month.

Syntax

The following is the syntax for using the time.gmtime() function.

time.gmtime(seconds)

Example

Here in this example we are going to pass the number seconds as the input argument to the gmtime() function of the time module then it returns the conversion of year, day and month.

import time
def calculate_date(seconds):
   time_tuple = time.gmtime(seconds)
   day = time_tuple.tm_mday
   month = time_tuple.tm_mon
   year = time_tuple.tm_year
   return day, month, year
seconds = 1706472809 
day, month, year = calculate_date(seconds)
print("The day, month and year from the given seconds:",day, month, year) 

Output

The day, month and year from the given seconds: 28 1 2024

Using the divmod() function

The divmod() function takes any two real numbers as the input arguments and returns the tuple with quotient and remainder of the given numbers.

Syntax

The below is the syntax for divmod() function.

quotient,remainder = divmod(val1,val2)

Example

In this example we are creating a function with the name calculate_name which takes seconds as the input argument. In the function we use the divmod() function. The year starts from 1970 so we have to check if the year is leap year or not and generate the days and months. Next it returns the day, month and year as the output.

def calculate_date(seconds):
   minutes, seconds = divmod(seconds, 60)
   hours, minutes = divmod(minutes, 60)
   days, hours = divmod(hours, 24)
   year = 1970
   while days > 365:
      if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
         if days > 366:
            days -= 366
            year += 1
         else:
            break
      else:
         days -= 365
         year += 1
   month_days = [31, 28 + (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
   month = 1
   while days >= month_days[month - 1]:
      days -= month_days[month - 1]
      month += 1
   return days + 1, month, year
seconds = 1706472809  
day, month, year = calculate_date(seconds)
print(day, month, year)  

Output

28 1 2024

Updated on: 19-Oct-2023

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements