Reputation: 773
I have a VSTO Outlook add-in. This add-in shows winforms window with DateTimePicker contorl, it is used to choose time of day. I want to set time format similar to current Windows time format (like in clock from taskbar).
To do this I use following code:
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
In Windows Region and Language settings I have French in Format and Location and English in Keyboards and Languages.
As a result, in my form I see 7:00 instead of 19:00.
CurrentCulture, CurrentUICulture and InstalledUICulture returns "h:mm tt" shorttime format and culture name is en-US.
My question is how to determine that Windows shows time in French format (HH:mm) and show time in DateTimePicker in this format?
--edit
When I run following code in separate console application
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern);
it returns
HH:mm
HH:mm:ss
dddd d MMMM yyyy HH:mm:ss
In Outlook add-in similar code returns american format:
h:mm tt
h:mm:ss tt
dddd, MMMM dd, yyyy h:mm:ss tt
Outlook is setup to use default Windows language (English).
Upvotes: 3
Views: 1143
Reputation: 773
To get time in Windows format, show form in separate thread. When showing it in Outlook thread it takes Outlook culture settings.
Upvotes: 0
Reputation: 3453
Rather than using custom formats, can't you use..
this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Time;
This should show the time in the correct format (i.e. the one selected by the user)
-- edit
What do you get if you run the following?
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern);
With my 'region and language' region set to 'French (France)' I get this.
HH:mm
HH:mm:ss
dddd d MMMM yyyy HH:mm:ss
Upvotes: 1