0

I am trying to deduct one day from my given string date. When i try like below format it shows error.

string fdate="12/11/2014"//(dd/MM/yyyy) format
string date2 = (DateTime.Parse(fdate).AddDays(-1)).ToString("dd/MM/yyyy");

Please point out the mistake which I did...

Bruno Blane
  • 43
  • 1
  • 4
  • 14

1 Answers1

1

Your code might work depending upon the format of system datetime. try to use ParseExact method to parse string using format.

string fdate = "12/11/2014";
string date2 = (DateTime.ParseExact(fdate, "dd/MM/yyyy", CultureInfo.InvariantCulture).AddDays(-1)).ToString("dd/MM/yyyy");

More information

  • yup it works. May I know the difference between Parse and ParseExact?? – Bruno Blane Nov 15 '14 at 06:33
  • `Parse` consider the system format and parse given string and `ParseExact` accept the custom date format as second argument and parse exactly the format you have passed. –  Nov 15 '14 at 06:37
  • I have a string date in database, that I retrive like **"string todate=dr[1].ToString();"** and coverts this string date to integer like **"Int todate1=Convert.ToInt32(todate);"** But it shows error **"Input string was not in a correct format"** let me know thereason?? – Bruno Blane Nov 15 '14 at 07:36
  • @BrunoBlane what is the value of `todate` ? –  Nov 15 '14 at 08:46
  • **21/11/2014**(dd/MM/yyyy) format – Bruno Blane Nov 15 '14 at 08:49