Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Actually i used Datetime & date function in my solution. But i want to give some conditions to date function. I retrieve saved date from database now i want to add 1day in that day.e.g. saved date is 2015-02-04 & if i add one day in that date then logically it will 2015-02-05. i want the same. but it show error like
Operator + can not be applied to operand of type system.datetime.
Posted
Updated 6-Feb-15 20:31pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-15 3:26am    
What would be the meaning of such operand? You can do subtraction, of course...
—SA
Member 11221185 7-Feb-15 5:22am    
@SA i unable to subtract 1 day.

DateTime currentdate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
DateTime newdate;
newdate = currentdate.AddDays(1);
TextBox1.Text = newdate.ToString("dd/MM/yyyy");
 
Share this answer
 
That would be because you might be applying the + operator to the dateTime object. Well the + operator has been overloaded for using with DateTime objects, but that is to add two different dateTime objects, like this one

C#
var date1 = new DateTime();
var date2 = new DateTime();

var newDate = date1 + date2; // valid code


The operator overloading can be viewed and learnt more about here on MSDN[^].

What you need is a function of the DateTime object, known as AddDays(), which accepts double value for you to add the days to your current DateTime instance.

For example the following code,

C#
var dateTime = new DateTime();
dateTime.AddDays(1); // adds one day to the current instance.


For more, please go through this MSDN link[^].
 
Share this answer
 
v2
Comments
Member 11221185 7-Feb-15 5:13am    
i accept your answer but i unable to to add AddDays() function in application.
Afzaal Ahmad Zeeshan 7-Feb-15 5:35am    
What is the problem that you're encountering? Can you please show the error, it would be helpful for us to resolve your problem.
 
Share this answer
 
Comments
Rajesh waran 7-Feb-15 2:59am    
5*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900