timespan issue

P

parez

Hi all,

I have a TimeSpan which i get my subtracing a date from a date.

TimeSpan ts (Dim ts As TimeSpan)
ts = parsedDate.Subtract(DateTime.Now)

i get different values for Hours and TotalHours. In my case i was
geting -15 and -39.
The difference between two is 24 (i know it is obvious).
Also i can alsp tell that one use 00:00 and the other uses 23:59.

but why??


i using .net 1.1

thanks in advance
 
N

Nicholas Paldino [.NET/C# MVP]

parez,

You are going to have to give an actual code sample (it can't have Now
in it either, since that would not be a very definitive example, as the Now
property is always changing) as it's nearly impossible to tell based on what
you have shown.
 
P

Peter Duniho

[...]
i get different values for Hours and TotalHours. In my case i was
geting -15 and -39.
The difference between two is 24 (i know it is obvious).
Also i can alsp tell that one use 00:00 and the other uses 23:59.

but why??

I agree with Nicholas that for best results, you should post a
concise-but-complete sample of code that reliably reproduces the
behavior. Then someone can answer the question in a very specific way.

That said, the simple answer is that Hours and TotalHours are two
completely different things. If you'll read the documentation for each,
you'll find that Hours is restricted to values between -23 and 23. In
other words, it's the "hours place" of a complete span of time. On the
other hand, TotalHours _is_ the complete span of time, represented as
hours.

It's sort of like the difference between saying that something takes 90
minutes to complete (where 90 is the total minutes), versus that it will
complete in 1 hour and 30 minutes (where 30 is the "minutes place" of the
time span).

Pete
 
J

Jon Skeet [C# MVP]

parez said:
I have a TimeSpan which i get my subtracing a date from a date.

TimeSpan ts (Dim ts As TimeSpan)
ts = parsedDate.Subtract(DateTime.Now)

i get different values for Hours and TotalHours. In my case i was
geting -15 and -39.
The difference between two is 24 (i know it is obvious).

TotalHours gives the total hours within the timespan, so 3 days=72
hours.

Hours gives the hours modulo 24, i.e. in the range 0-23 for positive
timespans, or -23 to 0 for negative timespans.

So in this case your difference is 1 day and 15 hours, or a total of 39
hours.
Also i can alsp tell that one use 00:00 and the other uses 23:59.

Not at all sure what you mean by that.
 
P

parez

string dt = "06/12/2007";

TimeSpan ts;
String[] expectF = new string[] { "%M/%d/yyyy", "%M/%d/
yyyy %H:mm" };
System.Globalization.CultureInfo ci =
System.Globalization.CultureInfo.CurrentCulture;

DateTime parsedDate = DateTime.ParseExact(dt, expectF, ci,
System.Globalization.DateTimeStyles.None);

ts = parsedDate.Subtract(DateTime.Now);
Console.WriteLine("Total HOurs" + ts.TotalHours);
Console.WriteLine("HOurs" + ts.Hours);
 
T

Tom Porterfield

parez said:
Hi all,

I have a TimeSpan which i get my subtracing a date from a date.

TimeSpan ts (Dim ts As TimeSpan)
ts = parsedDate.Subtract(DateTime.Now)

i get different values for Hours and TotalHours. In my case i was
geting -15 and -39.
The difference between two is 24 (i know it is obvious).

Hours and TotalHours represent different things on the TimeSpan object.
Hours is the number of hours for the instance. TotalHours is the
total value of the timespan object expressed in hours. Since your
TotalHours is -39, that is greater than one day (24 hours in a day). In
that case, I would expect your Days value to be -1 and your hours value
to be -15. That, when converted to hours should match your integer part
of your total hours - (-1*24) + (-15) = -39.
Also i can alsp tell that one use 00:00 and the other uses 23:59.

To be more precise the other uses 23:59:59.999999. While there are 24
hours in a day, once you pass 23:59:59.999999 you start a new day.
 
P

parez

Thanks for the reply.

I think i get it now. i will read the documentation properly next
time before posting.

This is why i got confused.This is from MSDN.

TimeSpan.Hours Property
Gets the number of whole hours represented by this instance.


TimeSpan.TotalHours Property
Gets the value of this instance expressed in whole and fractional
hours.

My only excuse is that english is my 3rd language. :)
 
P

Peter Duniho

[posted code]

And?

Nicholas already asked you to not use DateTime.Now, and there's nothing
about the code you posted that suggests what issue you're having.
Finally, what about the answer I already provided does not explain the
issue to you?

If you want a more useful answer, you need to give us more to go on.

Pete
 
P

Peter Duniho

This is why i got confused.This is from MSDN.

TimeSpan.Hours Property
Gets the number of whole hours represented by this instance.


TimeSpan.TotalHours Property
Gets the value of this instance expressed in whole and fractional
hours.

But in addition to that, it also says:

The hour component of the current TimeSpan structure.
The return value ranges from -23 through 23.

And:

A TimeSpan value can be represented as [-]d.hh:mm:ss.ff,
where the optional minus sign indicates a negative time
interval, the d component is days, hh is hours as measured
on a 24-hour clock, mm is minutes, ss is seconds, and ff
is fractions of a second. The value of the Hours property
is the hours component, hh
My only excuse is that english is my 3rd language. :)

While I don't know what your native language is, the MSDN documentation is
available in a wide variety of translations. There's a good chance that
includes your native language.

Pete
 
P

parez

Sorry about that and thanks for the reply.
My questions have been answered.

[posted code]

And?

Nicholas already asked you to not use DateTime.Now, and there's nothing
about the code you posted that suggests what issue you're having.
Finally, what about the answer I already provided does not explain the
issue to you?

If you want a more useful answer, you need to give us more to go on.

Pete
 
P

parez

But in addition to that, it also says:

The hour component of the current TimeSpan structure.
The return value ranges from -23 through 23.

That is why I said "i will read the documentation properly next
time before posting."
And:

A TimeSpan value can be represented as [-]d.hh:mm:ss.ff,
where the optional minus sign indicates a negative time
interval, the d component is days, hh is hours as measured
on a 24-hour clock, mm is minutes, ss is seconds, and ff
is fractions of a second. The value of the Hours property
is the hours component, hh
My only excuse is that english is my 3rd language. :)

While I don't know what your native language is, the MSDN documentation is
available in a wide variety of translations. There's a good chance that
includes your native language.

And no,it is not available in Konkani.

Paresh
 
M

Michael S

parez said:
And no,it is not available in Konkani.

Hehe, maybe Microsoft should use that as a threat to their coders:
- If you screw up one more time, we'll give you the task of translating the
MSDN library to Konkani!

Happy Coding
- Michael S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top