.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

add the specified number of days to current date in c#

Posted By: Mayank     Posted Date: December 26, 2011    Points:5   Category :C#
i have one calander control,one text box,lable and one button control .
when i select a date from calander and insert a number in textbox than it have to show the next date.
in these days friday not included.

please reply Its urgent

Responses
Author: Narayanan             
Posted Date: December 27, 2011     Points: 20   

Hi
try this .it may be useful to your task:
Declaration Part:
#region declaration Part
public static string CurrnetDate;
public static int cday;
public static int currentYear;
public static string currentmonth;
public static int nday;
public static int nextmonth;
public static int nextyear;
public static string NextDate;
public static string weekday;
public static string Day;
public static string year;
#endregion declaration Part
First get date format in Long Date string:
Declare this Code in Datetimepicker valuechanged
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
CurrnetDate = dateTimePicker1.Value.ToLongDateString();
string[] splitLongstring = CurrnetDate.Split(',');
weekday = splitLongstring[0];
string month = splitLongstring[1];
year = splitLongstring[2];
string[] splitmonth = month.Split(' ');
currentmonth = monthno(splitmonth[1]);
string day = splitmonth[2];
string nextday = selectdate(day, currentmonth, year);
textBox1.Text = nextday;
}

Check Leap year
#region Check leap year
public static bool chkleap(bool chk)
{
string[] test = splitdate();
int year = Convert.ToInt32(test[3]);
if (IsLeapYear(year))
{
return true;
}
else
{
return false;
}
}
#endregion Check leap year

#region select month end date
public static string selectdate(string sdate,string smonth,string syear)
{
if (currentmonth == "01" || currentmonth == "03" || currentmonth == "05" || currentmonth == "07" || currentmonth == "08" || currentmonth == "10" || currentmonth == "12")
{
if (currentmonth == "12" && sdate == "31" && weekday == "Thursday")
{
int nday = Convert.ToInt32(sdate);
int ndate = nday + 2;
sdate = ndate.ToString();
int nmonth = 1;
smonth = "0" + nmonth.ToString();
int cyear = Convert.ToInt32(year) + 1;
syear = cyear.ToString();
}
else if (currentmonth == "12" && sdate == "31" && weekday != "Thursday")
{
int nday = 1;
sdate = "0" + nday.ToString();
int nmonth = 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year) + 1;
syear = cyear.ToString();
}
else if (currentmonth == "12" && sdate != "31")
{
if (weekday != "Thursday")
{
int nday = Convert.ToInt32(sdate) + 1;
sdate = nday.ToString();
int nmonth = Convert.ToInt32(smonth);
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();

}
else if (weekday == "Thursday")
{
int nday = Convert.ToInt32(sdate) + 2;
sdate = nday.ToString();
int nmonth = Convert.ToInt32(smonth);
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
}
else if (currentmonth != "12" && sdate == "31" && weekday != "Thursday")
{
int nday = 1;
sdate = "0" + nday.ToString();
int nmonth = Convert.ToInt32(smonth) + 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
else if (currentmonth != "12" && sdate == "31" && weekday == "Thursday")
{
int nday = 2;
sdate = "0" + nday.ToString();
int nmonth = Convert.ToInt32(smonth) + 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
else if (Convert.ToInt32(sdate) < 31 && weekday == "Thursday")
{
int nday = Convert.ToInt32(sdate) + 2;
sdate = nday.ToString();
int nmonth = Convert.ToInt32(smonth);
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
else if (Convert.ToInt32(sdate) < 31 && weekday != "Thursday")
{
int nday = Convert.ToInt32(sdate) + 1;
sdate = nday.ToString();
int nmonth = Convert.ToInt32(smonth);
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
}
else if (currentmonth == "04" || currentmonth == "06" || currentmonth == "09" || currentmonth == "11")
{
if (sdate == "30" && weekday == "Thursday")
{
int nday = 2;
sdate = "0" + nday.ToString();
int nmonth = Convert.ToInt32(currentmonth) + 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
else if (sdate == "30" && weekday != "Thursday")
{
int nday = Convert.ToInt32(sdate)+1;
sdate = nday.ToString();
int nmonth = Convert.ToInt32(currentmonth) + 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
else if((Convert.ToInt32(sdate) < 30 && weekday == "Thursday"))
{
int nday = Convert.ToInt32(sdate) + 2;
sdate = nday.ToString();
int nmonth = Convert.ToInt32(currentmonth) ;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();

}
else if ((Convert.ToInt32(sdate) < 30 && weekday != "Thursday"))
{
int nday = Convert.ToInt32(sdate) + 1;
sdate = nday.ToString();
int nmonth = Convert.ToInt32(currentmonth) ;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();

}
}
else if (currentmonth == "02")
{
if (chkleap(true))
{
if (sdate == "29" && weekday == "Thursday")
{
int nday = 2;
sdate = "0" + nday.ToString();
int nmonth = Convert.ToInt32(currentmonth) + 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
else if (sdate == "29" && weekday != "Thursday" || sdate == "28" && weekday == "Thursday")
{
int nday = 1;
sdate = "0"+ nday.ToString();
int nmonth = Convert.ToInt32(currentmonth)+ 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
else if (Convert.ToInt32(sdate) < 29 && weekday != "Thursday")
{
int nday = Convert.ToInt32(sdate) + 1;
sdate = nday.ToString();
int nmonth = Convert.ToInt32(currentmonth);
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
}
else
{
if (sdate == "28" && weekday == "Thursday")
{
int nday = 2;
sdate = "0" + nday.ToString();
int nmonth = Convert.ToInt32(currentmonth) + 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
else if (sdate == "28" && weekday != "Thursday" || sdate == "27" && weekday == "Thursday")
{
int nday = 1;
sdate = "0" + nday.ToString();
int nmonth = Convert.ToInt32(currentmonth) + 1;
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
if (Convert.ToInt32(sdate) < 28 && weekday != "Thursday")
{
int nday = Convert.ToInt32(sdate) + 1;
sdate = "0" + nday.ToString();
int nmonth = Convert.ToInt32(currentmonth);
string monle = nmonth.ToString();
if (monle.Length > 1)
{
smonth = nmonth.ToString();
}
else
{
smonth = "0" + nmonth.ToString();
}
int cyear = Convert.ToInt32(year);
syear = cyear.ToString();
}
}
}
string result = sdate + "/" + smonth + "/" + syear;

return result;
}

#endregion select month end date
#region get month number
public static string monthno(string month)
{

switch (month)
{
case "January" :
month = "01" ;
break;
case "February":
month = "02";
break;
case "March":
month = "03";
break;
case "April":
month = "04";
break;
case "May":
month = "05";
break;
case "June":
month = "06";
break;
case "July":
month = "07";
break;
case "August":
month = "08";
break;
case "September":
month = "09";
break;
case "October":
month = "10";
break;
case "November":
month = "11";
break;
case "December":
month ="12";
break;

}
return month;

}
#endregion get month number




Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend