TimeSpan object question

K

kellygreer1

I haven't worked with the TimeSpan object before. So bare with me if
this seems like a newb question. But if I wanted to know how many
days from the current date until 2/4/2008. I have written this much
code so far.

DateTime startDate = new DateTime("2008-02-04");
DateTime curDate = DateTime.Now();
TimeSpan ts = startDate.Subtract(curDate);

Now I get lost. I was expecting to find a Days property off of the
TimeSpan object. No such luck. Where am I going wrong?

Thanks,
Kelly Greer
(e-mail address removed)
change nospam to yahoo
 
P

Peter Duniho

[...]
Now I get lost. I was expecting to find a Days property off of the
TimeSpan object. No such luck. Where am I going wrong?

Did you actually look at the properties of the TimeSpan class? Did you
happen see any property with the word "days" in it?

Hint: there are actually _two_ such properties, either of which might be
useful to you, depending on what exactly you need.

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

Kelly,

The Days property should be accessible through the ts variable. It's
definitely there. What makes you think it is not?
 
J

Jon Skeet [C# MVP]

kellygreer1 said:
I haven't worked with the TimeSpan object before. So bare with me if
this seems like a newb question. But if I wanted to know how many
days from the current date until 2/4/2008. I have written this much
code so far.

DateTime startDate = new DateTime("2008-02-04");
DateTime curDate = DateTime.Now();
TimeSpan ts = startDate.Subtract(curDate);

Note that it's slightly simpler with operator overloading - and you
don't need string parsing (which is fraught with issues):

DateTime startDate = new DateTime(2008, 2, 4);
TimeSpan ts = startDate - DateTime.Now;
Now I get lost. I was expecting to find a Days property off of the
TimeSpan object. No such luck. Where am I going wrong?

I'm not sure - TimeSpan definitely does have a Days property, and
always has had. What happens when you try to use ts.Days?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



Jon Skeet said:
I'm not sure - TimeSpan definitely does have a Days property, and
always has had. What happens when you try to use ts.Days?

I believe that the correct property is TimeSpan.TotalDays
 
P

Peter Duniho

I believe that the correct property is TimeSpan.TotalDays

It depends on what the OP wants. If they just want the integral days
portion of the TimeSpan, Days is correct.
 
K

kellygreer1

It depends on what the OP wants. If they just want the integral days
portion of the TimeSpan, Days is correct.

What I ended up doing was taking A ticks minus B ticks ... and then
dividing by the TimeSpan.TicksPerDay.
I'll fix this up tomorrow.

Wait. I just figured it out. Guess I was struck stupid today. I was
looking at the static side of TimeSpan and not an instance of
TimeSpan. Subtract 10 cool points. lol

Thanks guys,
Kelly
 

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

Top