Sign in to follow this  
orbano

[.net] DateTime fomrat string problem

Recommended Posts

Hi! I want the following datetime format: yyyy.MM.dd HH:mm:ss the problem is that the ":" means the default datetime separator in the actual collate. this can lead to problems when making queries from a machine with different collate than the one the mssql szerver has. (for example in italian, the ":" would mean "." that is not accepted by the mssql server having my local (hungarian) collate. Is there any way to make the format string always put ":"-s betveen time values? I've tried using "\" to make ":" be displayed as a literal, but did not work. any suggestions? (i would like to deal with the format string instead of redefining the datetime separators in the cultureinfo. thats way too complicated for the way i need to use this thing) thank you in advance! orbano

Share this post


Link to post
Share on other sites
You could use the datetime.ToString(string,IFormatProvider) method overload. When specifying the IFormatProvider , just get a new instance of the invariant culture.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathtrap
You could use the datetime.ToString(string,IFormatProvider) method overload. When specifying the IFormatProvider , just get a new instance of the invariant culture.


okay, will try. so you say there is no way to tell the format string to display a simple ":"?

Share this post


Link to post
Share on other sites
Well if you're going to be dealing with multiple locales, and storing things in a database, I'd convert everything from whatever locale to the invariant culture before storing it in a database. Then reconverted to whatever locale when displaying that data to the user.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathtrap
Well if you're going to be dealing with multiple locales, and storing things in a database, I'd convert everything from whatever locale to the invariant culture before storing it in a database. Then reconverted to whatever locale when displaying that data to the user.


yes thats another solution, and will work when rewriting my database classes, but at the moment i need a quick and simple solution to fix those ":"s :)
I dont want to use string.format, thats way too complicated now. Hope there is a way to cheat ":"s into that ####in' datetime format string :)

Share this post


Link to post
Share on other sites
A quick-and-very-dirty workaround is to set the current thread's culture to the invariant one, thusly;

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
A quick-and-very-dirty workaround is to set the current thread's culture to the invariant one, thusly;

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;


And it will ensure i have ":" between time values?

Is it really this hard to put ":" into a datetime string as a literal? :) Sometimes i really dont understand .NET design. (the other one i hate is the inconsistency between MYSSQL datetime and .NET datetime. One starts from 1900, the other one from 0001. And .NET datetime also lacks an extremal value (that can be mapped to dbnull). making each datetime field nullable is almost as beautiful as setting the thread's culture temporally to InvariantCultture :)

Sometimes i hate .NET the way i like it other times... It's a true love... :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this