Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
In my application, I have mentioned a custom datetime in my web.config file under <appSettings> tag as shown below.
XML
<appSettings>
<add key="DateTime" value="01/01/0001 01:01:01"/>
</appSettings>

So that time I want to add with current datetime in code behind.

Can any one say how to do this?

Thanks
Posted
Updated 28-May-11 0:59am
v2
Comments
Kim Togo 28-May-11 7:55am    
Hi Subudhi

Please use "Add Comment" under a solution for questions and answers or else nobody will get an email notify about you question.

What do you expect to get when you add two dates?
What is 3rd March + next Tuesday equal to? I don't know, and I don't think you do. Because it is silly. It is like adding oranges to lightyears!

Think about it: what are you actually trying to achieve?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-May-11 13:34pm    
Sure, a 5.
--SA
Paulo Pinto 1-Oct-12 14:22pm    
Not quite. Sometimes, in older databases, a date is stored in one field and time is stored in another field. So we need to add the date to the time and since both variables are DateTime, it makes perfect sense to add them.
Sergey Alexandrovich Kryukov 1-Oct-12 14:43pm    
I don't think you understand. You never add a date to a date. (Essentially, there is no such thing as date, per se; the date is just a partial information on some point it time.) You actually have some time, and add some time duration to it (TimeSpan), and this is not exactly the same. In your case: you have one date, which is actually a point in time, and assume the hour-minute-second-ms part to be 00:00:00:000, and then you add your separate time to it (from another field), and add to the original time. Can you see the difference? This answer is still valid.
--SA
You use DateTime[^] and TimeSpan[^] structs

But you cannot add a DateTime to another DateTime. You can add a TimeSpan to a DateTime.

C#
DateTime now = DateTime.Now;
DateTime arrive = now + new TimeSpan(2, 30, 0); // Add 2 hours and 30 min.


Change add key="DateTime" to TimeSpan.
 
Share this answer
 
Comments
Kim Togo 28-May-11 7:53am    
Moved to Comment from OP:
Hi Kim Togo and Abhinav S for your nice responses.

@ Kim Togo,

I got that this thing I can do in code behind. But how can I mention that timespan in web.config file and from there I want to retrieve the value and after that I will add that to the current datetime.
Is it possible?
Kim Togo 28-May-11 7:57am    
Sorry, I do not quite understand.

You can save a TimeSpan value in web.config file and then use this as an offset for DateTime.Now.
Sergey Alexandrovich Kryukov 28-May-11 13:35pm    
Correct, my 5.
--SA
Kim Togo 28-May-11 14:40pm    
Thanks SA
Read this date as described here[^].
Then add this value to another date using the DateTime.Add method.
 
Share this answer
 
Comments
Kim Togo 28-May-11 7:52am    
Moved to Comment from OP:

Hi Kim Togo and Abhinav S for your nice responses.

@ Kim Togo,

I got that this thing I can do in code behind. But how can I mention that timespan in web.config file and from there I want to retrieve the value and after that I will add that to the current datetime.
Is it possible?
Sergey Alexandrovich Kryukov 28-May-11 13:35pm    
You cannot add date to date; it makes no sense.
--SA

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