Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert day of year 1

Status
Not open for further replies.

ecannizzo

Programmer
Sep 19, 2000
213
US
I need to convert the day of the year (which I have) to a month and a day. Is there a function that will do this for me, or anyone know of a way?

Thanks!
 
create an array of months.

Code:
int months[12];

determine if it is a leap year and set months[1] appropriately.

int x = 0;

int i;
for(i = 0;i<12;i++)
{
   x += months[i];
  
   if(x> value_in_days)
       break;
}

ok... so we know the month is at i

now we need to figure out what day it is.

that should just be x-value_in_days;


I think that is the gist of it.

Matt

 
Thanks for your help but I'm a little confused. Lets say I know its day 300 of year 2001 I use month[300]?
 
No each element in the month array contains the days per month. So just search through the array (using that loop he wrote) until you find your day :).

-CDudd
 
The code below will meet your needs:

CTime tm(2001,1,1,0,0,0);
tm += CTimeSpan(300,0,0,0);
int nYear = tm.GetYear();
int nMonth = tm.GetMonth();
int nDay = tm.GetDay();
 
When I try to include afx.h I get linking errors. Any idea why?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top