Tutorial :Replace date in a DateTime with another date



Question:

How do you change the day part of a DateTime structure?

I have a DateTime with the value of 3/31/2009 8:00:00 AM and would like to change it to any other date but the time should be same. I don't need a solution to add day(s), I need to replace it with any arbitrary date.

How do I achieve this?


Solution:1

day = 1  month = 4  year = 2009    Date2 = new DateTime(day,month,year,Date1.Hours,Date1.Minute,Date1.Seconds)  


Solution:2

To construct a DateTime from two other DateTimes, one of which contains the date and one of which contains the time, you can use:

DateTime result = date.Date + time.TimeOfDay;  


Solution:3

s = s.AddDays(1)


Solution:4

You should use DateTime.AddDays method. Like this:

DateTime s2 = s.AddDays( 1 );  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »