Ways to set Second to zero in a DateTime?

  • Thread starter Thread starter Jen
  • Start date Start date
J

Jen

So I have the need to take UtcNow and change the Second to zero. Just
wondering how many ways there are to do that?
 
So I have the need to take UtcNow and change the Second to zero. Just
wondering how many ways there are to do that?

I would not be so bold as to put an upper bound on the number of ways to
do what you ask.

However, a couple of simple ways you might want to consider include:

* Use the DateTime.AddSeconds() method to add the negative of the
seconds property to the instance, which will set the seconds to zero

* Use one of the many constructors that exist for the DateTime
structure that include the seconds, copying all of the fields from an
initialized copy of DateTime.UtcNow except for the seconds field, which
you'll set to 0.

I don't doubt that there are many other alternatives, most of which are
probably a lot more complicated than the above. :)

Pete
 
Thanks, I did the New DateTime method. Kind of verbose but still only one
line of code.
 

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