Recent Posts

Thursday, December 3, 2009

Problems converting string date to Date or Date time in .net

I was having issue publishing my web site in the Server its regional settings were different to mine and my Asp .net website was giving me following error

" String was not recognized as a valid DateTime "

Since it was working fine in my computer and in debugging mode.

It wasted my 2 hours and finally i changed the code to following to solve the problem

dt = New DateTime(Convert.ToInt16(strYr), Convert.ToInt16(strMon), Convert.ToInt16(strDay))

or if date and time both is required then

dt = New DateTime(Convert.ToInt16(strYear), Convert.ToInt16(strMonth), Convert.ToInt16(strDay), Convert.ToInt16(strHr), Convert.ToInt16(strMin), 0)

This would run without error no matter what regional settings is in the server so it is simpe code but pretty handy. Enjoy coding

Related Posts by Categories




No comments:

Post a Comment