TimeSpan object question

  • Thread starter Thread starter kellygreer1
  • Start date Start date
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
 
[...]
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
 
Kelly,

The Days property should be accessible through the ts variable. It's
definitely there. What makes you think it is not?
 
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?
 
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
 
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.
 
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

Back
Top