0

I have an error "string was not recognized as a valid datetime". I took this error when add "datetimepicker" in my project. How can I solve this? I tried other solutions but didn't help.

Datetime garanti_baslangici = DateTime.Parse(
    GarantiBaslangici.Text, System.Globalization.CultureInfo.InvariantCulture);

.aspx code

<asp:TextBox ID="GarantiBaslangici" CssClass="datepicker" runat="server" />
krillgar
  • 12,596
  • 6
  • 50
  • 86
fable
  • 161
  • 14
  • 1
    Which control are you talking about? There are different controls for ASP.NET MVC and WebForms. All of them though provide the selected value as a `DateTime` value. You don't need to parse the text. – Panagiotis Kanavos Oct 31 '16 at 10:55
  • @PanagiotisKanavos I add .aspx code . I cant use value because of this is a textbox? – fable Oct 31 '16 at 10:58
  • 2
    I think that this should work - as long as the text entered represents a valid datetime. You are trying to parse the string with an invariant culture (which basically means UTC time) but your datepicker might be returning the string in the local culture. So if it returns, say, "10/31/2016" rather than "31/10/2016" you'll get this error. – ChrisF Oct 31 '16 at 11:03
  • That's not a datetimepicker, that's just a textbox with some javascript plugin on top. Which plugin are you using? Either the plugin returns the value in ISO8601 format, or you need to find the exact format. In both cases you need to use `DateTime.ParseExact` or `DateTime.TryParseExact` – Panagiotis Kanavos Oct 31 '16 at 11:19
  • For ISO8601 you can try the predefined `s` or `o` formats, `YYYY-MM-DDTHH:mm:ss` or `YYYY-MM-DDTHH:mm:ssZ`. The various options are described [in this possibly duplicate SO question](http://stackoverflow.com/questions/3556144/how-to-create-a-net-datetime-from-iso-8601-format) – Panagiotis Kanavos Oct 31 '16 at 11:25
  • @ChrisF This answer helped me for understand clearly. Thank you for that. – fable Oct 31 '16 at 11:53

1 Answers1

1

Try just parse string, it should parse right according to the current system format.

Datetime garanti_baslangici=DateTime.Parse(GarantiBaslangici.Text) 

or better

Datetime garanti_baslangici;
DateTime.TryParse(GarantiBaslangici.Text,garanti_baslangici);
HoTTab1CH
  • 199
  • 3
  • 20
  • That's what caused the problem in the first place - assuming that the text follows some format. This would *only* happen if both the web browser and the server used the same culture which is rarely the case – Panagiotis Kanavos Oct 31 '16 at 11:17
  • It uses user's regional system format, so there shouldn't be any problems. And if some user will use other format you can just show error message and ask to write correct format. It's almost impossible to predict how it should be and and what user will write, so user need write according to his system format.. – HoTTab1CH Oct 31 '16 at 11:28
  • Which user's? The browser user's or the server's? Why do you assume the text is in a specific format ? – Panagiotis Kanavos Oct 31 '16 at 11:30
  • Browser user. I don't. For example user system date format is dd.mm.yy if user will write something like 08.25.2016 it will throw error, and we will need to show error message and ask user to write in correct format. – HoTTab1CH Oct 31 '16 at 11:45
  • This answer helped me thank you. I guess this error cause is "invariant.culture". – fable Oct 31 '16 at 11:51