Date Time conversion problem. Plz Help

  • Thread starter Thread starter Wajih-ur-Rehman
  • Start date Start date
W

Wajih-ur-Rehman

I want to convert a DateTime Object to a string and the conversion should be
such that it should work irrespective of locale of the current system.

I have used the following code to accomplish this:

CultureInfo c = new
CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, true);
DateTimeFormatInfo d = c.DateTimeFormat;

d.DateSeparator = "/";

// dt is a DateTime object


string finalString = dt.ToString("MM/dd/yyyy hh:mm:ss tt",d);

but unfortunately it is not running correctly on all locales. For example;
it fails for locale Czech. When i say fail, i mean that while inserting this
date in the database an exception is thrown by the DB that its not a valid
format for DateTime. Any idea what i am doing wrong? or how i can make it
work independent of locale. I would really appreciate any help.

Regards

Wajih
 
Wajih-ur-Rehman said:
but unfortunately it is not running correctly on all locales. For example;
it fails for locale Czech. When i say fail, i mean that while inserting this
date in the database an exception is thrown by the DB that its not a valid
format for DateTime. Any idea what i am doing wrong? or how i can make it
work independent of locale. I would really appreciate any help.

When you come to insert a date into the database, you shouldn't use a
string format at all - you should use a parameter, set the type
appropriately, and use the DateTime value itself.
 
Wajih-ur-Rehman said:
I want to convert a DateTime Object to a string and the conversion should be
such that it should work irrespective of locale of the current system.

I have used the following code to accomplish this:

CultureInfo c = new
CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, true);
DateTimeFormatInfo d = c.DateTimeFormat;

d.DateSeparator = "/";

// dt is a DateTime object


string finalString = dt.ToString("MM/dd/yyyy hh:mm:ss tt",d);

but unfortunately it is not running correctly on all locales. For example;
it fails for locale Czech. When i say fail, i mean that while inserting this
date in the database an exception is thrown by the DB that its not a valid
format for DateTime. Any idea what i am doing wrong? or how i can make it
work independent of locale. I would really appreciate any help.

Regards

Wajih

You're specifying an explicit date format, it's inherently not
culture-neutral since you're specifying it! :)

If you're trying to insert stuff into the database, you shouldn't be
formatting things yourself, you should be using parameterized queries
such that the provider takes care of marshalling the data types back and
forth.

For example, instead of:

"SELECT * FROM Employee WHERE start_date > '" + (new
DateTime(2003,1,1,0,0,0)).ToString() + "'";

Use:

"SELECT * FROM Employee WHERE start_date > @startDate";

Then, in your SqlCommand add a parameter:

SqlCommand cmd = ....

cmd.Parameters.Add("startDate", new DateTime(2003,1,1,0,0,0));

-c
 
Thanx for helping me out with this. There is one more question. In case of
access, the dates have to be enclosed in # while in MySQL and MSSQL, the
dates are enclosed in '

With the way that you have described, it seems that I don't even have to
enclose the dates in # or '. It will be automatically done by the provider.
Is this understanding correct?

Regards

Wajih
 
Wajih-ur-Rehman said:
Thanx for helping me out with this. There is one more question. In case of
access, the dates have to be enclosed in # while in MySQL and MSSQL, the
dates are enclosed in '

With the way that you have described, it seems that I don't even have to
enclose the dates in # or '. It will be automatically done by the provider.
Is this understanding correct?

Yes. Using parameters shields you from everything like that, and string
quoting, etc.
 
Back
Top