Changing the value of a DateTime

  • Thread starter Thread starter billygotee
  • Start date Start date
B

billygotee

Hi,

Okay this is taking longer to figure out than I thought it would. The
integer members of a DateTime (such as DateTime.Minutes,
DateTime.Seconds, etc.) can only get the value, not set it. Is there
a way to set one of these without parsing a string?

What I want to do is clear out the Minutes, Seconds, and Milliseconds
values so that the smallest resolution allowed in the DateTime (for my
application) is an hour. Any method of concatenating these values
would be very helpful!

Thanks,
Brandon
 
[...]
What I want to do is clear out the Minutes, Seconds, and Milliseconds
values so that the smallest resolution allowed in the DateTime (for my
application) is an hour. Any method of concatenating these values
would be very helpful!

You can either use the "Add" methods to adjust the DateTime object by the
negative of its current values for those fields, or you can just use an
appropriate constructor to create a new DateTime object where the fields
you want set to zero are in fact set to zero.

For example:

DateTime dtCur = DateTime.Now();

dtCur = new dtCur(dtCur.Year, dtCur.Month, dtCur.Day, dtCur.Hour,0,
0);

Pete
 
Okay this is taking longer to figure out than I thought it would. The
integer members of a DateTime (such as DateTime.Minutes,
DateTime.Seconds, etc.) can only get the value, not set it. Is there
a way to set one of these without parsing a string?

What I want to do is clear out the Minutes, Seconds, and Milliseconds
values so that the smallest resolution allowed in the DateTime (for my
application) is an hour. Any method of concatenating these values
would be very helpful!

DateTime instances are immutable, meaning that once created, the value
they contain cannot be changed.

Your best be is somethin like this:
myDateTime = new DateTime( myDateTime.Year, myDateTime.Month,
MyDateTime.Day, MyDateTime.Hour );

HTH
Andy
 
DateTime instances are immutable, meaning that once created, the value
they contain cannot be changed.

Your best be is somethin like this:
myDateTime = new DateTime( myDateTime.Year, myDateTime.Month,
MyDateTime.Day, MyDateTime.Hour );

HTH
Andy

Perfect. MSDN is useless sometimes -- thanks gentlemen
 
[...]
What I want to do is clear out the Minutes, Seconds, and Milliseconds
values so that the smallest resolution allowed in the DateTime (for
my
application) is an hour. Any method of concatenating these values
would be very helpful!

You can either use the "Add" methods to adjust the DateTime object by
the negative of its current values for those fields, or you can just
use an appropriate constructor to create a new DateTime object where
the fields you want set to zero are in fact set to zero.

Peter's recommendation is on the money, but just as a clarification, to
avoid confusion.
The following statement is not accurate as written:
use the "Add" methods to adjust the DateTime object by the negative of
its current values for those fields

The Add method will create a new Datetime object that incorporates your
changes.
It will not modify the existing DateTime object at all.

Bill
 
Peter's recommendation is on the money, but just as a clarification, to
avoid confusion.
The following statement is not accurate as written:

Sorry for the confusion...though, I think "not accurate" isn't quite as
fair as "poorly written". :)

My point was simply that the "Add" methods start with the initial date. I
didn't mean to imply that the object would be modified in-place.

Pete
 
Peter Duniho said:
Sorry for the confusion...though, I think "not accurate" isn't quite
as fair as "poorly written". :)

My point was simply that the "Add" methods start with the initial
date. I didn't mean to imply that the object would be modified
in-place.

I didn't mean to imply that you misunderstood anything, so I hope it
didn't come across that way.

It is just that there is often a great deal of confusion surrounding how
many of the DateTime method actually work. Many people that are new to
dotnet don't immediately realize that DateTimes are immutable.

Sorry for any confusion
Bill
 
[...]
It is just that there is often a great deal of confusion surrounding how
many of the DateTime method actually work. Many people that are new to
dotnet don't immediately realize that DateTimes are immutable.

Yes, very true. Thank you for the clarification.
 

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