简体   繁体   中英

Error after publishing : String was not recognized as a valid DateTime asp.net

My project works fine when it runs on my localhost but after publishing to hosting service, it gives error on every place where I used DateTime.ParseExact

string date = "04-03-2019";
DateTime dt = DateTime.ParseExact(date, "dd-MM-yyyy", CultureInfo.InvariantCulture);

I also tried DateTime dt = DateTime.ParseExact(date, "dd-MM-yyyy", null);

I think using appropriate culture for your date format resolve this.

If your input date format is "dd-MM-yyyy", try the following code.......

string strDate = "04-03-2019";
DateTime dt = Convert.ToDateTime(strDate,
System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);

Here "hi-IN" Culture will identify your date string in "dd-MM-yyyy" format.

检查文化是否设置为英国格式,例如 dd/mm/yyyy(英国格式),因为还有另一种格式,例如 mm/dd/yyyy(美国),因为您使用本地主机检查您电脑上的时间格式

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