Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, July 2007
can any one suggest regarding to Date operations


Message 1 in thread

my problem....
    iam having two dates lets suppose d1(1st date) & d2(2nd date).. i want to find defference between that todates 
and the result should again in date format....
example:
             d1 = 23-09-1983;
             d2 = 23-08-1984;
then result(d2 - d1)..that  iam expecting 00-11-0000(dd-mm-yyyy)
is there any functions in QDate module to complete that task...

my solution is:
       QDate d1(23,9,1983);
        QDate d2(23,8,1983);
        QDate d3(0,0,0);

        nDays = d1.daysTo(d2);

        d3 = d3.addDays(nDays);

strDate =   QString(" %1-%2-%3").arg( d3.year()).arg( d3.month()).arg(d3.day());
but strDate is not getting correct value ,what i desired....
what wrong with me..
        
D. Anil kumar

Every success has a story of great failure.
So don’t stop with failure where
Success comes after failure.

Message 2 in thread

Hi There

> my solution is:
>        QDate d1(23,9,1983);
>         QDate d2(23,8,1983);
>         QDate d3(0,0,0);
> 
>         nDays = d1.daysTo(d2);
> 
>         d3 = d3.addDays(nDays);
> 
> strDate =   QString(" %1-%2-%3").arg( d3.year()).arg(
> d3.month()).arg(d3.day());
> but strDate is not getting correct value ,what i desired....
> what wrong with me..


You need to read the docs for QDate. It's very hard (impossible?) to
create a '0' date - from the docs:

<snip>
QDate::QDate ( int y, int m, int d )
Constructs a date with year y, month m and day d.
If the specified date is invalid, the date is not set and isValid()
returns false. Any date before 2 January 4713 B.C. is considered
invalid.

Warning: For historical reasons, if y is in the range 0 to 99, it is
interpreted as a year in the range 1900 to 1999. New code should not
rely on this behavior as it might be changed in a future release.
</snip>

That warning is the important part. Plus, I'm pretty sure that years
before 1600 or so are in the Julian Calendar, which is almost definitely
not what you want.

I'd suggest storing your difference in an integer (just store the number
of days difference). IMHO it doesn't make sense to store the difference
between two dates in a traditional dd-mm-yyyy format (due to the
variances in the traditional calendar year).


I hope this help!



-- 
 [ signature omitted ] 

Message 3 in thread

anil kumar wrote:
> my problem....
>     iam having two dates lets suppose d1(1st date) & d2(2nd date).. i
want
> to find defference between that todates
> and the result should again in date format....
> example:
>              d1 = 23-09-1983;
>              d2 = 23-08-1984;
> then result(d2 - d1)..that  iam expecting 00-11-0000(dd-mm-yyyy)

calculating the difference of two dates in this way doesn't make much
sense.
the reason for this is, that "1 month" may be equal to as many as 28 to
31 days, and a year may be equal to 365 or 366 days.

So the way that QDate calculates the difference (in days) is really the
only "correct" way to do it.

Cheers,
Peter

--
 [ signature omitted ] 

Message 4 in thread

On 7/4/07, Peter Prade <prade@xxxxxxxxxxx> wrote:
>
> anil kumar wrote:
> > my problem....
> >     iam having two dates lets suppose d1(1st date) & d2(2nd date).. i
> want
> > to find defference between that todates
> > and the result should again in date format....
> > example:
> >              d1 = 23-09-1983;
> >              d2 = 23-08-1984;
> > then result(d2 - d1)..that  iam expecting 00-11-0000(dd-mm-yyyy)
>
> calculating the difference of two dates in this way doesn't make much
> sense.
> the reason for this is, that "1 month" may be equal to as many as 28 to
> 31 days, and a year may be equal to 365 or 366 days.
>
> So the way that QDate calculates the difference (in days) is really the
> only "correct" way to do it.


It should be noted that you can safely use weeks as well, e.g., "5 weeks and
4 days", since a week is always seven days.

While I agree with the other repliers who point out all of the faults with
trying to calculate the difference in terms of years and months, I can think
of a couple cases where it might be desirable. It isn't unusual for someone
to say, "He's 1 year and 3 days older than me". Also, many subscription
services are based on months, so it may make sense to say, "You have been a
member for 4 years and 6 months" (in this case in particular, the number of
days or weeks would be meaningless to a user). However, in both these cases
the users wouldn't know how to read 00-06-0004, so you would have to do some
post-processing anyways. I'd be interested in hearing why you want the
difference in this format.

Tom