Solved

Difference between dates

Posted on 2004-04-04
16
401 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
0
Comment
Question by:santoshv25
  • 5
  • 4
  • 2
  • +3
16 Comments
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10755128
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
 
LVL 11

Expert Comment

by:avizit
ID: 10755259
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10755436
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
 
LVL 17

Expert Comment

by:mokule
ID: 10755466
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
 
LVL 17

Expert Comment

by:mokule
ID: 10755470
Sorry. Should be.
Not the difference in time decide but the number of day.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10764160
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
 
LVL 17

Expert Comment

by:mokule
ID: 10764235
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
 
LVL 20

Expert Comment

by:a_b
ID: 10791849
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:ssnkumar
ID: 10803501
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
 

Author Comment

by:santoshv25
ID: 10820973
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10821079
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
 
LVL 17

Expert Comment

by:mokule
ID: 10821431
There is no good answer for Your question as long as You don't define what is a month for You.
0
 

Author Comment

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

forget months/days..
0
 
LVL 8

Accepted Solution

by:
ssnkumar earned 20 total points
ID: 10821691
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
 
LVL 17

Expert Comment

by:mokule
ID: 10821848
> 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
 
LVL 20

Expert Comment

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


a_b
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now