I have a datetime value in a CSV file and below is the sample data:
20/06/2016 11:52
21/06/2016 11:52
22/06/2016 11:52
Non when I try to parse this datetime, I get error:
String was not recognized as a valid DateTime.
I am not sure what will be the format of this date but I would always like to parse it in that culture in which my application will be using. So based on current culture I would like to parse my date.
This is how I am trying but getting error as above:
string row = "20/06/2016 11:52"
Try 1:
CultureInfo culture = CultureInfo.CurrentCulture;
DateTimeStyles styles = DateTimeStyles.None;
DateTime dateValue;
DateTime.TryParse(rowValue, culture, styles, out dateValue); // {1/1/0001 12:00:00 AM}
Try 2
DateTimeFormatInfo usDtfi = new CultureInfo(culture.Name, false).DateTimeFormat;
var l = Convert.ToDateTime(rowValue, usDtfi); //String was not recognized as a valid DateTime
var g = DateTime.Parse(rowValue, usDtfi);//String was not recognized as a valid DateTime
All this above approches are failing and I would like to have exact date and want to store in my SQL Server database table.
My system datetime is in format : mm/dd/yy
I have already seen some questions like below:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
Datetime format Issue: String was not recognized as a valid DateTime
But all those answers are specifying date format but I don't know what will be the format; that is why I am trying to detect from current culture. I am not sure whether I am thinking in a right way.