Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
#include<conio.h>
#define DAYSINYEAR 365
#define DAYSINMONTH 30
#define DAYSINWEEK 7
void main()
{
   int year=0, month=0, week=0, day=0;
   clrscr();
   printf("Enter the number of days:");
   scanf("%d",&day);
   while(day>=DAYSINYEAR)
   {
     day = day-DAYSINYEAR;
     year++;
   }
   while(day>=DAYSINMONTH)
   {
     day = day-DAYSINMONTH;
     month++;
   }

 while(day>=DAYSINWEEK)
 {
   day = day-DAYSINWEEK;
   week++;
 }
 printf("\n%d year, %d Month, %d Weeks and %d Days", year,month,week,day);
// return 0;
 getch();
}


how can i convert simple integer value to years, months and days with taking care of leap year, and also 31 day of months.
like i have entered 364 then answer will be 11 months and 30 days..
or if entered 366 then it should be 1 year, 0 month and 1 day..
please help me. i have already the simple code but it is not giving me correct answer.
it is giving the answer if i entered 364 the 12 months and 4 days so its wrong but it seems right for big amount of value. so i need the exact code..
the code is given above.

Thanx
Posted
Updated 3-Mar-14 4:11am
v2
Comments
Krunal Rohit 3-Mar-14 10:05am    
put that code here.

-KR
Vijay Pareek 3-Mar-14 10:13am    
my question is having code now check it out....
Vedat Ozan Oner 3-Mar-14 10:06am    
send your code, then let's talk about it.
Vijay Pareek 3-Mar-14 10:13am    
my question is having code now check it out....
Stefan_Lang 3-Mar-14 10:17am    
There is no solution to this, as months and years do not have a fixed length. The result of the conversion will always depend on what months and what year you are looking at!

Likely you've misinterpreted the task you've been given, or if you've come up with it by yourself, you should reconsider what is the goal of that calculation. For instance banks like to average month to day ratios for the purpose of calculating day-to-day inertest rates.

1 solution

There is no "direct" solution: you can't have a "months" count without knowing the start date, including the year - as the length of February changes every four years.

But... if you want to accept inaccurate answers which assume a 30 day month as in your code, then it's easier than your think - sort of.
C++
years = days / 365;
days = days % 365;
months = days / 30;
days = days % 30;
Since you are using integers throughout, the divide throws away any fractions, then the modulus throws away the bit you just calculated!

But do be aware: it produces rubbish results in the real world: such as 1 year, 12 months, 4 days if you enter 729!
 
Share this answer
 
Comments
Stefan_Lang 3-Mar-14 10:55am    
_Of_course_ it's rubbish - how do you think banks earn money? ;-p
OriginalGriff 3-Mar-14 11:17am    
I think they hose it out of my account :sigh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900