-1

I have some problem for which I am unable to find the solution. Here is my code

string DateString = System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
string Format = "dd/MM/yyyy hh:mm:ss tt";
DateTime DT = DateTime.ParseExact(DateString, Format, CultureInfo.InvariantCulture);
string DTE = DT.ToString("dd/MM/yyyy");

The code is extracting an Error which I am pasting below. Please tell me what exactly is the problem (My System Date and Time has been set to 3/29/2018 like this and I don't want to change it at all) here is the error

Error Picture

  • 2
    Which line of code give the error? Did you debug? The code you shared and the stack trace of the exception do not match. – Chetan Mar 29 '18 at 18:45
  • Did you try running this code in Console application ? – Chetan Mar 29 '18 at 18:46
  • Yes its looks like you are write. please help me, when i debug this application in this current page it doesnt show any error on any line but once after its finish all the codes (i check it line by line their is no error in any line) its goes to global.asax and show this error which i have pasted. – Zaigham Ali Mar 30 '18 at 15:17
  • moreover if i correct my system date to dd/mm/yyyy format it does not give any kind or error, its looks like that the PC format of date has something to do with it. i do not have any problem in changing the date format in my system but when upload this to IIS and load the website from web sever it changes the date format to m/d//yyyy., is their any way to change the date format in IIS to mm/dd/yyyy? please update. Please help this is the only hurdle in the application left. – Zaigham Ali Mar 30 '18 at 15:17
  • @ Chetan Ranpariya Please help me in this matter. – Zaigham Ali Mar 31 '18 at 12:22

1 Answers1

0

During DateTime conversion to string, you should use CultureInfo.InvariantCulture also. If you do not this, DateString is converted as your machine culture, but you convert back again culture invariant. This may leads problem.

For example: when I use your code, DateString has "OS" value agains to tt field, after than I try to convert into DateTime with CultureInvariant, OS cannot be solved.

string DateString = System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string Format = "dd/MM/yyyy hh:mm:ss tt";
DateTime DT = DateTime.ParseExact(DateString, Format, CultureInfo.InvariantCulture);
string DTE = DT.ToString("dd/MM/yyyy");
Adem Catamak
  • 1,987
  • 2
  • 17
  • 25