We help IT Professionals succeed at work.

Difference between dates

santoshv25
santoshv25 asked
on
Medium Priority
441 Views
Last Modified: 2010-04-15
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
Comment
Watch Question

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

-ssnkumar

Commented:
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
Top Expert 2004

Commented:
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

Commented:
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.

Commented:
Sorry. Should be.
Not the difference in time decide but the number of day.
Top Expert 2004

Commented:
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

Commented:
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
a_b
Top Expert 2009

Commented:
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.

Commented:
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

Author

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...

Commented:
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

Commented:
There is no good answer for Your question as long as You don't define what is a month for You.

Author

Commented:
i am giving a date -> 20040416
and i want the return value as 20020504...
that is it....

forget months/days..
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
> 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.
a_b
Top Expert 2009

Commented:
The function I had defined for you works fine , jsut change the iteration value from 4 to 24


a_b
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.