• C

Difference between dates

I am writing program in C which should do the following :

check with a date 4 months back...for ex -> to find out data which lies between current date and 4 months back.
if data not found ,search for remaining 22 months..

how to achive that ?

do reply
santoshv25Asked:
Who is Participating?
 
ssnkumarConnect With a Mentor Commented:
If you are not concrened about the exact day, then it is very easy!
date = 20040416;
Divide this to 3 parts day, month and year. (For this you will have to write a small function, which I think you can write)
After calling that function, it will become:
day = 16;
month = 4;
year = 2004;
You you want to go back 24 months.
So do like this:
new_day = day;
new_month = month - 24;
New_year = year;
if (new_month == 0)
{
    new_month = 12;
    new_year--;
}
else if (new_month < 0)
{
    while (new_month < 0)
    {
        new_month = new_month + 12;
        new_year = new_year - 1;
    }
}

new_date = (new_year * 10000) + (new_month * 100) + new_day;

After all this, your new_date will be:
20020416

Hope this helps.......

-ssnkumar
0
 
ssnkumarCommented:
Can you make the question more clear?
What is data and what is its format?
How do you link data to time-date?

-ssnkumar
0
 
avizitCommented:
I might have got your  question wrong , but  from what i gather

1. you need to write a function diffDate(date1, date2) which gives the number of days between two dates { include suitable validations for valid date etc }

2. call this function with the date you want and current date

check if the difference is
less than 4 months ,
greater than 4 months but less than 22 months

etc etc

anyway you need to be clear bout what you want.

--Abhijit
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
stefan73Commented:
Hi santoshv25,
> check with a date 4 months back...for ex -> to find out data which lies
> between current date and 4 months back.
> if data not found ,search for remaining 22 months..

C time_t functions return the number of seconds since 1/Jan/1970 0:00 am. So you require three steps:

time_t today,four_months_back;

today=time(NULL);
today -= today % 86400; /* Truncate to 0:00 am, day is 86400 seconds */
four_months_back = today - 4*31*86400;

That's still very crude, but it will give you a starting point. Also have a look at struct tm functions, such as localtime().


1. Get time_t via time()
2. Truncate to full day:
   

Cheers,
Stefan
0
 
mokuleCommented:
I wouldn't do it like this.
In most situations I know the rule for comparing month distance is as follows:

for date d1/m1/y1 >= d1/m2/y2

monthdiff = 12*(y1-y2)+(m1-m2)+(d1<d2 ? -1: 0)

not the difference in time decide but the number of date.
But it depends on application of course.
0
 
mokuleCommented:
Sorry. Should be.
Not the difference in time decide but the number of day.
0
 
stefan73Commented:
mokule,
> the rule for comparing month distance is as follows

Before we dive into wild speculations:

The logic of "four months back" has to be exactly defined first. This is normally by business or legal requirement. Depending on this exact definition, we can then develop an algorithm.

You'll stumble into all sorts of problems otherwise. Imagine the rule "same day of month, just 4 months back" for the 31st of July. Ouch.

Stefan
0
 
mokuleCommented:
Stefan,

Please note the beginning of my sentence
>In most situations I know ..

I absolutely agree that
>The logic of "four months back" has to be exactly defined first.

My purpose was to show another point of view.

Kind Regards,
Marek
0
 
a_bCommented:
Try to keep things simple

Write a func  :
int no_of_days ( int date, int month, int year)
{
int arr_months[12][1];
int temp=0;
for(int i=0;i<12;i++)
 months[i][0]=i+1;  
arr_months[0][0]=31;
arr_months[1][0]=28;
arr_months[2][0]=31;
arr_months[3][0]=30;
arr_months[4][0]=31;
arr_months[5][0]=30;
arr_months[6][0]=31;
arr_months[7][0]=31;
arr_months[8][0]=30;
arr_months[9][0]=31;
arr_months[10][0]=30;
arr_months[11][0]=31;
months=months-1;
for(i=1;i<=4;i++)
  {  
     if((months-1)<0)
        months=11;
     else months=months-1;
     temp=temp+arr_months[months];
  }
return temp;
}
The above function will reurn the exact no of days in the past four months
Run a loop in the main program for these no of days over your data to find data.

You can write a similar case for 22 mmonths.

Pls take care of the special cases -
 1. Leap yr for Feb
 2. Resolve case of dates of 31st

If you could elaborate on how is your data stored I might be able to give a more precise solution.
0
 
ssnkumarCommented:
a_b,

>arr_months[0][0]=31;
How do you know his month is begining from January!?
Why don't you ask for clarifications and after getting that, you can suggest as many suggestions as you would like to!
Otherwise, your suggestions go waste, if the askers intention was different!

-ssnkumar
0
 
santoshv25Author Commented:
what i want is -> new logic i want now..

i will pass the current date : 2004/04/14
and i want the date 24 months back...how to achieve that ? may be 2002/05/14 ??

do reply to me as soon as possible with the code...
0
 
ssnkumarCommented:
Hi Santosh,
 
   You can try this approach.
   You will have to work backwards.
   get the current time using time function and store it in t1:
t1 = time(NULL);

   Next, you have to find out how many seconds have elapsed in last 24 months. (If it is always multiple of 12, then it will be easier. If not then you will have to keep count of the number of days in each month).
   So, you will have to try to find out, if the last year and the one previous to that was a leap year.
   If it is leap year, then no of days will be 366, otherwise 365.
now t2 = no_of_days * 24 * 60 * 60;

t3 = t2 - t1.
Now use ctime() function to convert t3 to ascii representation of date.

Hope this helps......

-ssnkumar
0
 
mokuleCommented:
There is no good answer for Your question as long as You don't define what is a month for You.
0
 
santoshv25Author Commented:
i am giving a date -> 20040416
and i want the return value as 20020504...
that is it....

forget months/days..
0
 
mokuleCommented:
> i am giving a date -> 20040416
> and i want the return value as 20020504...

I don't undertstand it.
You must either specify all possible input date and output date :)
or tell what is month for You.
0
 
a_bCommented:
The function I had defined for you works fine , jsut change the iteration value from 4 to 24


a_b
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.