# [.net] DateTime fomrat string problem

This topic is 4127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Quote:
 Original post by deathtrapYou 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 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 on other sites
Quote:
 Original post by deathtrapWell 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 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 on other sites
Quote:
 Original post by benryvesA 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

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 10
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999987
×