String to DateTime Conversion in C #

Silly questions, but I can’t get around it ... I have a line in this format 20081119

And I have a C # method that converts a string to DateTime for input in SQL Server DB

public static DateTime MyDateConversion(string dateAsString) { return System.DateTime.ParseExact(dateAsString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); } 

The problem is that Date looks like this: Date = 11/19/2008 12:00:00 AM, and I need it to be a DateTime of type yyyyMMdd, since I map it to the schema to call the stored procedure.

Thanks in advance guys.

Cheers con

+6
c #
source share
5 answers

There is no such thing as a "DateTime of type yyyyMMdd"; DateTime is just a large integer indicating the amount of time in an era - it has no format. But this is normal, since you should still use parameterized TSQL, so just add a DateTime as the DbParameter value and it will be passed to db in an unambiguous way (do not use string concatenation to create the TSQL command):

 DbParameter param = cmd.CreateParameter(); param.ParameterName = "@foo"; param.DbType = DbType.DateTime; param.Value = yourDateTime; // the DateTime returned from .ParseExact cmd.Parameters.Add(param); 

or for SqlCommand :

 cmd.Parameters.Add("@foo", SqlDbType.DateTime).Value = yourDateTime; 

If you really need a string, just use the string directly as the parameter [n] [var] char.

In addition, in this case, I will use an invariant culture for parsing the date (since the culture is not displayed in the format):

 DateTime yourDateTime = DateTime.ParseExact(dateString, "yyyyMMdd", CultureInfo.InvariantCulture); 

From the conversation, it seems you might also need to go from DateTime to a string, in which case just cancel it:

 string dateString = yourDateTime.ToString("yyyyMMdd", CultureInfo.InvariantCulture); 
+13
source share

Date Time is the class that defaults ToString format as 11/19/2008 12:00:00 AM

This is from MSDN , which can help you.

Since the appearance of a date and time value depends on factors such as culture, international standards, application requirements, and personal preferences, the DateTime structure offers a lot of flexibility in date formatting and time values ​​through overloading its ToString method. By default, the DateTime.ToString () method returns a string representation of the date and, using the current culture, a short date and a long period of time. The following example uses the default DateTime.ToString () method to display the date and time using a short date and a long time for the en-US culture, the current culture on the computer on which the example was run.

Thus, you can overload ToString in DateTime in the desired format, otherwise transfer the string representation directly to the stored procedure instead

+1
source share

Okay, back to the culture ... When you say:

The date is displayed as follows: Date = 11/19/2008 12:00:00 AM

I assume you are using ToString on a date to see this result? The formatting in ToString will depend on the culture and will use your current culture by default.

I was able to reproduce the format you get by doing this:

 var dateString = "20081119"; var fr = new System.Globalization.CultureInfo("fr-FR"); var resultingDate =DateTime.ParseExact(dateString,"yyyyMMdd",System.Globalization.CultureInfo.CurrentCulture); Console.WriteLine(resultingDate.ToString(fr)); 

You have a valid date, so formatting does not matter, but if that happens, and you need to get it in the format you described, then you need to format it when converting to a string ... if it is already a string, then there is no need to convert the date .

I might have misread your question, but I needed to get it because it was eavesdropping on me.

+1
source share

I think about it because of the culture set in CurrentCulture, not knowing what it is, I cannot be sure, but the en-US indication works on my end. Here is the code I have:

 var dateString = "20081119"; var enUS = new System.Globalization.CultureInfo("en-US"); var resultingDate = DateTime.ParseExact(dateString,"yyyyMMdd",enUS); Console.WriteLine(resultingDate.ToString()); 

Try it and see if this works for you.

0
source share

This is what will give the exact result you are looking for:

convert (varchar (10), getdate (), 112): datetime to string (format YYYYMMDD)

convert (datetime, '20081203', 112): string to datetime (format YYYYMMDD)

Code Side:

DateTimeFormatInfo fmt = (new CultureInfo ("hr-HR")). DateTimeFormat; Console.WriteLine (thisDate.ToString ("d", fmt)); // Displays 03/15/2008 (use similar formats to suit your requirements)

or

date1.ToString ("YYYYMMDD", CultureInfo.CreateSpecificCulture ("EN-US")) date1.ToString ("YYYYMMDD");

Details at http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

0
source share

All Articles