Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey friends i am facing a problem please help me.

I have Start date and End Date as well as i have NoofDaysDifference which could be 1,2,3,5,7,10,12,15,18,20,25,30.

I have to get current date on the basis No of (1 or 2 or 3 or 5 or 7 or 10 like wise) NoofDaysDifference in month/total duration.

Current Date should be decrement by NoofDaysDifference.

I have done for every one days like this
C#
int NoofDays;

NoofDays = dtEnd.Subtract(dtstart).Days;

while (dtEnd.Date > dtstart.Date && NoofDays >= 0)
{
   DateTime CurrentDate = dtstart.AddDays(NoofDays);
   NoofDays--;
}
Posted
Updated 5-Sep-13 20:29pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Sep-13 3:02am    
And what's the problem?
—SA
OriginalGriff 6-Sep-13 3:51am    
That doesn't make much sense, even with your added comment.
Remember, we can't see your screen, or access your HDD, or read your mind, so we only get to work with exactly what you tell us.
Perhaps if you gave us an example or two? With a start date, end date, NoofDays value and what you expect to get at the end so we can work out what you are trying to achieve? And explain what is wrong with your current code?
Use the "Improve question" widget to edit your question and provide better information.

Your question is not clear. Here are two examples of date computations. Do either of these come close to your requirements? If not, please use Improve Question and show a few examples using real dates that will demonstrate what you are trying to do.


Example 1
C#
StringBuilder strOutput = new StringBuilder(4096);
DateTime dtStart = DateTime.Parse("2013-09-01");
DateTime dtEnd = DateTime.Parse("2013-09-05");
int NoOfDays=3;
DateTime dt;
dt = dtStart.Date;
while (dt <= dtEnd)
{
    strOutput.AppendLine(dt.ToString("yyyy-MM-dd"));
    dt=dt.AddDays(NoOfDays);
}
Console.WriteLine(strOutput);

Output
2013-09-01
2013-09-04


Example 2
C#
DateTime dtStart = DateTime.Parse("2013-09-01");
DateTime dtEnd = DateTime.Parse("2013-09-05");
int NoOfDays;
DateTime dt;
NoOfDays = dtEnd.Subtract(dtStart).Days;
dt = DateTime.Now.Date.AddDays(-(double)NoOfDays);
Console.WriteLine("Start: " + dtStart.ToString("yyyy-MM-dd") + ", End: " + dtEnd.ToString("yyyy-MM-dd"));
Console.WriteLine("Days Difference: " + NoOfDays );
Console.WriteLine("Now=" + DateTime.Now.Date.ToString("yyyy-MM-dd") + ", Computed Date=" + dt.Date.ToString("yyyy-MM-dd"));


Output
Start: 2013-09-01, End: 2013-09-05
Days Difference: 4
Now=2013-09-06, Computed Date=2013-09-02

Note: A time value is included in the value stored in the DateTime Class. You can use string formatting when displaying a DateTime Class value to not display the time portion.
 
Share this answer
 
v9
if (ddlScheduledMedicines.SelectedIndex == 1)
{
daysdifference = 3;
}
else if (ddlScheduledMedicines.SelectedIndex == 2)
{
daysdifference = 5;
}
else if (ddlScheduledMedicines.SelectedIndex == 3)
{
daysdifference = 7;
}

DateTime CurrentDate =dtstart.Date;

while (CurrentDate<dtEnd)
{
CurrentDate = CurrentDate.AddDays(daysdifference);
}
 
Share this answer
 
Comments
Mike Meinz 6-Sep-13 6:34am    
You have posted this as a solution to the question. You posted the original question. Are you posting a solution to your own question? Are you trying to clarify your question? If you are trying to clarify your question, please delete this Solution and use the Improve Question link above to put into your question some actual yyyy-MM-dd dates for the input values and expected output values so that we can see what you are actually trying to accomplish.

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