• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

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.

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21865500.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"?
0
M3hcSS
Asked:
M3hcSS
  • 4
  • 3
  • 2
1 Solution
 
dstanley9Commented:
DataTime myNewDate = new DateTime(myDate.Year, 10, myDate.Day, myData.Hour, myDate.Minute, myDate.Second, myDate.Millisecond);
0
 
dstanley9Commented:
oops... typo:

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

DateTime myNewDate = new DateTime(myDate.Year, 10, myDate.Day, myDate.Hour, myDate.Minute, myDate.Second, myDate.Millisecond);
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
mrichmonCommented:
The only way I know of to do that is to create a new DateTime object using the portions you want and not the parts you don't want.

ateTime myTimeOn=new DateTime(1753,1,1,1,1,1);

myTimeOn = new DateTime(myTimeOn.Year, 10, myTimeOn.Day, myTimeOn.Hour, myTimeOn.Minute, myTimeOn.Second);
0
 
M3hcSSAuthor Commented:
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.
0
 
dstanley9Commented:
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.
0
 
mrichmonCommented:
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.
0
 
M3hcSSAuthor Commented:
This does, indeed, work.

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

myVar=10;
myVar=20;

Or...

myVar=myVar++;
0
 
mrichmonCommented:
Not quite, but you can think of it that way.  ;o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now