Changing the datetime value

  • Thread starter Thread starter David Hubball
  • Start date Start date
D

David Hubball

Hi

Supposing a routine sets a time variable :-
DateTime dtMaxCountdown = new DateTime(2009,01,01,12,5,00);

Does anyone know how to to them alter the time from 5mins, 00 Seconds
to say 20 mins 00 Seconds.

I can use the Add method ok to add 20 minutes on but need could do
with knowing another way. (I'm doing a Countdown program so would like
and would like to avoid using the Add method to make my program more
flexible)

Much Appreciated if anyone knows of a way.

Thanks
David
 
David Hubball presented the following explanation :
Hi

Supposing a routine sets a time variable :-
DateTime dtMaxCountdown = new DateTime(2009,01,01,12,5,00);

Does anyone know how to to them alter the time from 5mins, 00 Seconds
to say 20 mins 00 Seconds.

I can use the Add method ok to add 20 minutes on but need could do
with knowing another way. (I'm doing a Countdown program so would like
and would like to avoid using the Add method to make my program more
flexible)

Much Appreciated if anyone knows of a way.

Thanks
David

It's not entirely clear what you mean.

If you want to set the minutes to 20 (regardless of the current value),
you could do something like this:
DateTime dt = new DateTime(2009,01,01,12,5,00);
dt = dt.AddMinutes(20-dt.Minute);

But for a countdown, you maybe want a TimeSpan:
TimeSpan ts = dtMaxCountdown - DateTime.Now;

You can't just change the Minute property on the DateTime: it's
immutable. You need to create a new one, copying the values you want to
keep.

What sort of flexibility do you mean?


Hans Kesting
 
Hi Hans

By saying I need more flexibility I mean't that just being able to add
and subtract time is not enough - I also need to be able to easily
reset the time back to 20 minutes without doing calculation first to
see how much time I need to add back. So I think you've given me more
ideas now - so I'll start looking into the TimeSpan type as I not
heard of that one before.

Thank v much
David
 

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