1

I am writing this code the text Change event but it is showing the error that is string is not valid.

 DateTime ts=Convert.ToDateTime(Joiningdate.Text);
   DateTime dt1=ts.AddMonths(6);
    txtcd.Text = dt1.ToShortDateString();
Ankur Gupta
  • 933
  • 3
  • 15
  • 27

2 Answers2

0

The DataTime format does not match the format you need to convert string to DateTime object. You can use DateTime.ParseExact() to give you format you date you have.

Assuming you have format dd/MM/YY for the textbox

DateTime ts= DateTime.ParseExact(Joiningdate.Text, "dd/MM/yy", CultureInfo.InvariantCulture);
DateTime dt1=ts.AddMonths(6);
txtcd.Text = dt1.ToShortDateString();
Adil
  • 146,340
  • 25
  • 209
  • 204
0

The problem is with the format of Joiningdate.Text
You will have to tell that in what format your date time is.

If your date time is in format use

DateTime ts= DateTime.ParseExact(Joiningdate.Text, "ddMMyyyy", 
                              CultureInfo.InvariantCulture);
DateTime dt1=ts.AddMonths(6);

nd then you can convert back to any format you want

ts.ToString("yyyyMMdd");


You can go through this link

Convert DateTime to string format("yyyyMMdd")

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117