[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Working with TimeSpan

musosdev

10/22/2008 6:19:00 PM

Hi guys

I need to do some calculations based on a TimeSpan object.

The TimeSpan object is created from StartTime and EndTime (both DateTime
objects), and contains for example "02:00" to indicate 2 hrs.

I need to perform the following calculation on the TimeSpan

TimeSpan/(1/24)

This works in Excel in a Date/Time field, but I can't find ANY ConvertTo
that will allow me to perform this calculation, presumably because the
TimeSpan includes : ??

I think I can get round it by converting it to string, and then doing the
calculation on each section, but I really wanted to perform it directly on
the TimeSpan if at all possible.

Can someone enlighten me as to what I need/can convert it to to be able to
perform the calculation.

Thanks


Dan
2 Answers

Peter Duniho

10/22/2008 7:33:00 PM

0

On Wed, 22 Oct 2008 11:19:01 -0700, musosdev <musoswire@community.nospam>
wrote:

> Hi guys
>
> I need to do some calculations based on a TimeSpan object.
>
> The TimeSpan object is created from StartTime and EndTime (both DateTime
> objects), and contains for example "02:00" to indicate 2 hrs.

No, it doesn't. You would only get "02:00" if you converted the TimeSpan
to a string. Until you do that, the TimeSpan is an exact representation
of some specific length of time.

> I need to perform the following calculation on the TimeSpan
>
> TimeSpan/(1/24)

Why?

> This works in Excel in a Date/Time field, but I can't find ANY ConvertTo
> that will allow me to perform this calculation, presumably because the
> TimeSpan includes : ??

In Excel, a time value is represented in terms of days or fractions of
days. If you wanted to convert a time value in Excel to hours, it would
not be unusual at all to multiply it by 24, as you've shown above.

But in .NET, if you have a TimeSpan value and you want to get the number
of hours it represents, you can just retrieve the TimeSpan.TotalHours
property.

Pete

musosdev

10/23/2008 8:19:00 AM

0

Peter,

Thanks for help... TotalHours is exactly what I needed to avoid the
calculation entirely!

Cheers


Dan

"Peter Duniho" wrote:

> On Wed, 22 Oct 2008 11:19:01 -0700, musosdev <musoswire@community.nospam>
> wrote:
>
> > Hi guys
> >
> > I need to do some calculations based on a TimeSpan object.
> >
> > The TimeSpan object is created from StartTime and EndTime (both DateTime
> > objects), and contains for example "02:00" to indicate 2 hrs.
>
> No, it doesn't. You would only get "02:00" if you converted the TimeSpan
> to a string. Until you do that, the TimeSpan is an exact representation
> of some specific length of time.
>
> > I need to perform the following calculation on the TimeSpan
> >
> > TimeSpan/(1/24)
>
> Why?
>
> > This works in Excel in a Date/Time field, but I can't find ANY ConvertTo
> > that will allow me to perform this calculation, presumably because the
> > TimeSpan includes : ??
>
> In Excel, a time value is represented in terms of days or fractions of
> days. If you wanted to convert a time value in Excel to hours, it would
> not be unusual at all to multiply it by 24, as you've shown above.
>
> But in .NET, if you have a TimeSpan value and you want to get the number
> of hours it represents, you can just retrieve the TimeSpan.TotalHours
> property.
>
> Pete
>