• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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