1

I am looking for code in .NET (C#) to change the system date/time format in Regional Options of the Control Panel for Windows XP.

Gabe
  • 84,912
  • 12
  • 139
  • 238
vrushali
  • 27
  • 1
  • 6
  • 2
    Are you sure you want to change it globally, for all applications on the computer? – Joe White May 18 '11 at 06:45
  • 2
    OB Old New Thing link [Don't use global state to manage a local problem](http://blogs.msdn.com/b/oldnewthing/archive/2008/12/11/9193695.aspx). If it doesn't apply, apologies, but it's frequently seen. – Damien_The_Unbeliever May 18 '11 at 08:12

4 Answers4

2

To change date-time format take a look at this http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/263d73b2-8611-4398-9f09-9aa76bbf325e/

You basically need to use native Win API method SetLocaleInfo.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • OP wants to change the display format of the date/time, not set the date/time. – Gabe May 18 '11 at 06:28
  • @Gabe - well, this is not what post said originally. It said 'how to change system datetime'. It didn't say 'format'. – Alex Aza May 18 '11 at 06:30
  • Yes, the post was a bit more confusing before I edited it. Since the word "format" is in the title and the format is what you change in the Regional Options control panel, I'm fairly certain that's what the OP meant. – Gabe May 18 '11 at 06:32
  • i will be used this syntax it will be run but it not changed the format it will be display the extra symbol in the time like 09::::00::::00 – vrushali May 18 '11 at 09:39
1

If you want to change temporarily the regional settings you can use following code:

System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

...

YOUR CODE

...

System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

TO find out which regional setting is best, check:

CultureInfo

More reading on System.Globalisation

Raghnall
  • 33
  • 6
1

If I understand right, you want to programatically change regional settings permanently, not just for your current process.

The information you need is stored in the registry. For the current user under the key:

HKCU\Control Panel\International

And the default for new users or users without a profile:

HKEY_USERS\.Default\Control Panel\International

You could change the registry values programatically, then broadcast a WM_SETTINGCHANGE message as described in a response to this question.

Community
  • 1
  • 1
Joe
  • 122,218
  • 32
  • 205
  • 338
  • is it possible to change the setting to only my process because datetime variable take system format & i want stored this date to the mysql ,MYSQL accept only the 'mm-dd-yyyy HH:mm:ss' format it will not accept the 'mm/dd/yyyy HH:mm:ss tt' this format i want to change this format then stored in to database in datetime format – vrushali May 18 '11 at 09:38
  • i will be update the this registry HKEY_USERS\.Default\Control Panel\International this change will be not affected in the regional settings in control panel – vrushali May 18 '11 at 12:10
  • @vrushali - is your *actual* problem about getting datetime values in your application to insert correctly into a MySQL database? If so, why didn't you ask that instead? It's got very different answers, and you don't end up breaking the users machine. – Damien_The_Unbeliever May 18 '11 at 13:11
  • when this key updated HKEY_USERS\.Default\Control Panel\International – vrushali May 19 '11 at 06:49
-1
public void changedatetimeformat()
{
    RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\International", true);
    regkey.SetValue("sShortDate", "dd/MM/yyyy");
    regkey.SetValue("sLongDate", "dd/MM/yyyy");
}

Then Restart Your System

CinCout
  • 9,486
  • 12
  • 49
  • 67
  • Please try formatting your answer for better readability and explain what your code does. You might also take a look at [the help center](https://stackoverflow.com/help/how-to-answer) to learn how to write better answers. – Jakob Sep 13 '17 at 09:57