Converting to a Common Date Format

Converting to a Common Date Format

I am trying to insert CSV data into SQL.

It has date column in the format ""

You can see space before starting date and I cannot change it as this csv is protected using Azure services.

I am getting this error shown below:

String was not recognized as a valid DateTime.Couldn't store <7/20/2021 6:16> in Last Seen Column. The expected type is DateTime.

I have done a logic to convert existing data formats as below

if (IsValidDateFormat("dd/MM/yyyy HH:mm", csvTable.Rows[i][LastSeen].ToString()))
                    {
                        dt.Rows.Add(csvTable.Rows[i][Computer].ToString()
                       , csvTable.Rows[i][LastSeen]);

                    }
                    else if (IsValidDateFormat("dd-MMM-yy HH:mm:ss", csvTable.Rows[i][LastSeen].ToString()))
                    {
                        dt.Rows.Add(csvTable.Rows[i][Computer].ToString()
                                          , DateTime.ParseExact(csvTable.Rows[i][LastSeen].ToString() + ",531", "dd-MMM-yy HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture).ToString());


                    }
                    else if (IsValidDateFormat("M/dd/yyyy h:mm", csvTable.Rows[i][LastSeen].ToString()))
                    {
                        dt.Rows.Add(csvTable.Rows[i][Computer].ToString()
                       , csvTable.Rows[i][LastSeen]);

                    }

But every day new date formats are coming in csv so that I need to change the code each time a new date format comes.

Question 1: Is there an easy way to make a common code for any date format instead of changing code for each date format?

Question 2: Since there is a space, I am getting the error, is there anyway I can trim the date?

The funny thing is I am able to import the data without any issues in windows10 laptop using visual studio, but the error which I have given above is only appears when I run the same deployed exe through windows scheduler in the Windows server 2012 machine

23

1 Answer

The issue got sorted out when I changed the line

DateTime.ParseExact(csvTable.Rows[i][LastSeen].ToString() + ",531", "dd/MM/yyyy HH:mm,fff", System.Globalization.CultureInfo.InvariantCulture).ToString()

and included culture info

if (IsValidDateFormat("dd/MM/yyyy HH:mm", csvTable.Rows[i][LastSeen].ToString()))
                    {
                        dt.Rows.Add(csvTable.Rows[i][Computer].ToString()
                       , **DateTime.ParseExact(csvTable.Rows[i][LastSeen].ToString() + ",531", "dd/MM/yyyy HH:mm,fff", System.Globalization.CultureInfo.InvariantCulture).ToString()**);
                    }
                    else if (IsValidDateFormat("dd-MMM-yy HH:mm:ss", csvTable.Rows[i][LastSeen].ToString()))
                    {
                        dt.Rows.Add(csvTable.Rows[i][Computer].ToString()
                                          , DateTime.ParseExact(csvTable.Rows[i][LastSeen].ToString() + ",531", "dd-MMM-yy HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture).ToString());


                    }
                    else if (IsValidDateFormat("M/dd/yyyy h:mm", csvTable.Rows[i][LastSeen].ToString()))
                    {
                        dt.Rows.Add(csvTable.Rows[i][Computer].ToString()
                       , DateTime.ParseExact(csvTable.Rows[i][LastSeen].ToString() + ",531", "M/dd/yyyy h:mm,fff", System.Globalization.CultureInfo.InvariantCulture).ToString());

                    }

Your Answer

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

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.