Problem converting or casting Date String to DateTime

I am using jQuery DatePicker to get the date for a text field in a gridview to insert / update records. The datepicker date is accepted as a string. for example, the value "07/31/2014".

Using the following code, I will convert the date string to C # DateTime .

  var sDate = ((TextBox)row.FindControlRecursive("iStartDateTBox")).Text; payment.StartDate = DateTime.ParseExact(sDate, "mm/dd/yyyy", null); 

However, after the conversion, the value set for the "Payment Start Date" property will become "01/31/2014 12:07:00". I am simply perplexed about what is actually happening. And I would like some tips to solve this problem.

0
source share
2 answers

You can use DateTime.ParseExact with IFormatProvider. ( MSDN )

So try something like this:

 var result = DateTime.TryParse( "10. 10. 2014", CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.None, out date); 
+1
source

It was just a matter of formatting the date of the date . When using DateTime the MM code refers to the month, where the minutes are MM ...

 payment.StartDate = DateTime.ParseExact(sDate, "mm/dd/yyyy", null); 

IT SHOULD BE

 payment.StartDate = DateTime.ParseExact(sDate, "mm/dd/yyyy", null); 
+1
source

Source: https://habr.com/ru/post/1435895/


All Articles