Solved

Using dtEnd.AddDays To Calculate Future Dates

Posted on 2014-01-07
4
241 Views
Last Modified: 2014-01-07
I have an app that includes a calendar that allows users to select dates.   The customer wants to allow the users to schedule their meetings with this web page.  The user selects a date (like today) from a calendar.  Then, they enter a number in a field.  The code needs to calculate today's date plus the number of years entered in the text box and display that as a date.  Example:  today plus 5 years = 1/7/2019.

Here is the code that I'm trying to use to perform the calculation:
  {
        DateTime myStartDate, myEndDate;
        myEndDate.AddDays  (this displays the date selected by the calendar)

Open in new window


Is there a tutorial or webpage that displays the proper formatting to do what I'm trying to do?  I've tried myEndDate.AddDays(1), myEnddate.addDays(-1), etc but nothing works.  As you can tell, I am new to this so any help is appreciated.
0
Comment
Question by:programmher
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
dustock earned 125 total points
ID: 39762605
As stated on MSDN

"This method does not change the value of this DateTime. Instead, it returns a new DateTime whose value is the result of this operation."

So the Add days is working, it just looks like you aren't doing anything with the value.  Try this...
myEndDate = myEndDate.AddDays(1);

Open in new window

0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 125 total points
ID: 39762638
I assume you do actually set a value into myEndDate before you use the AddDays function (in the way as previously pointed out you need to).

DateTime myStartDate, myEndDate;
//put a value into myEndDate at this point
        myEndDate = myEndDate.AddDays  (this displays the date selected by the calendar)
0
 

Author Comment

by:programmher
ID: 39762765
AndyAnscow - yes, there are values set before I use the AddDays function.
0
 

Author Comment

by:programmher
ID: 39762767
Dustrock - you are correct - my function was working.   I had someone validate that for me.  Thank you for your help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Search an image for an image 3 31
Example works but why do I need to click twice on button 4 43
MVC DDL Json Not Binding to Model 2 13
parsing JSON help 1 10
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question