Changing In DateTime Format

95 views Asked by At

I have changed format to dd-mm-yy in backend of page, i want to save date in'dd-mm-yy' format e.g.'23-02-2015' in database.and retreieve it in this format, i am using datetime picker in the text field...i changed the format of date picker to my 'dd-mm-yyyy' but its not saving data in that format and cause the below error

String was not recognized as a valid DateTime.

Any suggestions?

2

There are 2 answers

0
Royi Namir On

"String was not recognized as a valid DateTime."

First , why aren't you sending datetime object ?

If you can't(!) , well ,

If your datetime object id dt so do dt.ToString("yyyy-MM-ddTHH':'mm':'sszzz", DateTimeFormatInfo.InvariantInfo)

This will be a valid iso datetime string.

The only truly safe formats for date/time literals in SQL Server, at least for DATETIME and SMALLDATETIME, are:

YYYYMMDD
YYYY-MM-DDThh:nn[:ss[:mmm]]
0
Zohar Peled On

Simply don't use strings to represent dates.
the DateTime struct of c# maps to sql server's datetime data type directly, meaning that you can simply send the DateTime struct as a parameter value in your SqlCommand object (I hope you are using parameterized queries or stored procedure. If not, then start using them right now, unless you want to be vulnerable to Sql injection attacks.)

Read here and there about the difference between Display format and storing format of date values in sql server.