Link to home
Start Free TrialLog in
Avatar of M3hcSS
M3hcSS

asked on

How do I change the value of a datetime?

The more I thought about it, the more I realized that I asked the wrong question in the below link.

https://www.experts-exchange.com/questions/21865500/How-to-set-or-change-the-value-of-the-year-day-hour-minute-second-of-an-already-initialized-datetime-object.html

If I've already set the datetime values for the year, month, day, hour...etc..., then how do I reset the value?

Suppose I want to set the month value to "10". Can I do this without using the ".Add"?
Avatar of dstanley9
dstanley9

DataTime myNewDate = new DateTime(myDate.Year, 10, myDate.Day, myData.Hour, myDate.Minute, myDate.Second, myDate.Millisecond);
oops... typo:

DataTime myNewDate = new DateTime(myDate.Year, 10, myDate.Day, myDate.Hour, myDate.Minute, myDate.Second, myDate.Millisecond);
GRRR!

DateTime myNewDate = new DateTime(myDate.Year, 10, myDate.Day, myDate.Hour, myDate.Minute, myDate.Second, myDate.Millisecond);
ASKER CERTIFIED SOLUTION
Avatar of mrichmon
mrichmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of M3hcSS

ASKER

Wow, I find the fast response to be frightening. Thanks.

Yes, I keep running into this problem whereby I have to create a new object. I've already created the new datetime object so what I want to do is simply reset certain values.

The mrichmon is something I didn't think about. I don't have time right now to try it out but it sure looks nice.
DateTime is a structure, so there won't be much overhead w/ creating a new one.  All of the properties are set in the constructor, so there's no way to change a part of the DateTime.  You have to create a new one.
Both methods are doing the identical thing.

It is the same exact thing I mentionedto you in the other question.  You are creating a new date time object and then simply overwriting your original one with the new one.
Avatar of M3hcSS

ASKER

This does, indeed, work.

I think it is a little like overwriting a simple variable...

myVar=10;
myVar=20;

Or...

myVar=myVar++;
Not quite, but you can think of it that way.  ;o)