.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 >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Show Next Date in the Textbox using C#.net

Posted By:Narayanan       Posted Date: December 27, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Now, we discuss about to show next date in the Textbox when user selects in the DateTimepicker using C#.net
 

First ,get a value in Longdatestring format when user selects in Datetimepicker in C#.net.

CurrnetDate = dateTimePicker1.Value.ToLongDateString();

Before that declare a variable in the Coding window Top
 
#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
Now you check the year Leap year or Not:
#region To check year leap year
        public static bool IsLeapYear(int year)
        {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
                return true;
            else
                return false;
        }
        #endregion To check year leap year
Now you change to month name to month no (ex:"March" "03")
#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


Now you create method for result
#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

result part
    
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;
        }


     

Further Readings:

Responses
Author: Ajith.NET         Company URL: http://www.dotnetspark.com
Posted Date: December 31, 2011

Hello friend.. your code seems to be nice effort..
but there are 2 mistakes - the longdate formatting depends on the regional settings and will throw exception on different computers.

Secondly, u r not ware that DateTime.IsLeapYear() already exists, DateTime.Today.AddDays(1) returns the next day..

i am wondering what is the article about>
Author: Syed Shakeer Hussain         Company URL: http://www.dotnetspark.com
Posted Date: January 02, 2012

Hi Narayan,
To show nextDay in a textBox you can simply do it with one Line of code

eg:textbox1.Text=Convert.ToString(DateTime.Today.AddDays(1));
Author: netgrid         Company URL: http://www.dotnetspark.com
Posted Date: September 22, 2012

Nice article and I thought it was better from many of the other entries on the site.
http://www.dapfor.com/en/net-suite/net-grid/tutorial/headers-and-columns


Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

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