简体   繁体   中英

Error while Converting Date Using Convert.todatetime function.in asp.net

I have a date time field to be saved and updated..my system datetime format is dd/MM/yyyy after saving it in sql server ..while retrieving date im displaying it in MM/dd/yy format.

I have some other date textboxes also in which I am by default displaying the current date using function

System.DateTime.Now.ToShortTimeString();

or say

DateTime.Today.ToString("MM/dd/yyyy"); 

while updating data it gives me error

"string was not recognized as valid datetime"

on the line where im using Convert.Todatetime(Textbox.Text) Function in update method

/ is the replacement character for the current culture's date separator, so if you want to enforce it use CultureInfo.InvariantCulture :

DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture); 

See: The "/" Custom Format Specifier

Use ParseExcat

var dateTime = DateTime.ParseExact(Textbox.Text,
  "MM/dd/yyyy",CultureInfo.InvariantCulture);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM