0

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.

10
  • Maybe because there are no seconds. Use TryParseExact Commented Jan 4, 2017 at 15:02
  • 1
    Duplicate stackoverflow.com/questions/341175/… Commented Jan 4, 2017 at 15:04
  • Is the date you're parsing stored in your file in various formats? So you could have multiple dates in the file, each with a different format? Commented Jan 4, 2017 at 15:12
  • Take a look at the following method it supports multiple date formats for parsing. DateTime.ParseExact Method (String, String[], IFormatProvider, DateTimeStyles) msdn.microsoft.com/en-us/library/332de853(v=vs.110).aspx Commented Jan 4, 2017 at 15:12
  • @Rabid penguin:yes the date can have multiple formats so I want datetime to be stored based on my current culture Commented Jan 4, 2017 at 15:44

2 Answers 2

1

If you're sure that each string you encounter will be in a proper format, but you just don't know which one, one thing you can do is get an array of all the different formats on your machine and use that in the parse exact method:

var formats = (from CultureInfo ct in CultureInfo.GetCultures(CultureTypes.AllCultures)
               select ct.DateTimeFormat.GetAllDateTimePatterns()).SelectMany((x) => x).ToArray();
DateTime test = DateTime.ParseExact("20/06/2016 11:52", formats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);

That code on my machine generates over 26,000 formats. As long the string follows one of them it will be accepted.

If getting the proper format will be inconsistent you could go this route:

var formats = (from CultureInfo ct in CultureInfo.GetCultures(CultureTypes.AllCultures)
               select ct.DateTimeFormat);
string dateString = "20/06/2016 11:52";
DateTime temp = new DateTime(0);
foreach (DateTimeFormatInfo dfi in formats)
{
    if (DateTime.TryParseExact(dateString, dfi.GetAllDateTimePatterns(), dfi, DateTimeStyles.None, out temp))
    {
        break;
    }
}
if(temp == new DateTime(0))
{
    //save string to get it's format
}
Sign up to request clarification or add additional context in comments.

8 Comments

No actually I dnt know because date van be in dd/mm/yy format or can be in mm/dd/yy format.user could store datetime in fike in any format
My point was will it always be a proper format or say can it be like this 2006/2016 11:52 with a mistake in it?
Nope it will always be in format like this :6/20/2006 or 20/6/2016
Then the code, I first put up, should work, since as I pointed out, it generates over 26,000 formats to check the string against.
Ok upvoted for your kind efforts towards helping me.will check it and let you know. Actually I am not sure whether I am thinking in right direction or not
|
1

If you are 100% sure the format will always be the same, you can use the method ParseExact like:

var parsedDate = DateTime.ParseExact(row, "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);

3 Comments

See my updated question as because sometimes this will not be the format
Do you know what type of format it will vary between? Or could it be all kinds of formats?
See my datetime can be either in mm/dd/yy or it could be dd/mm/yy or it could be yy/dd/mm with hours minute ans second. So I want to store it in my sql table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.