Determine/Set Current User Short Date Format

  • Thread starter theinvisibleGhost
  • Start date
T

theinvisibleGhost

Is there a method of determining which format the short date format is
stored in for the current user on the machine using .NET?

If it is not in UK format I ideally need to be able to set it to UK
format as well.

I'm building an installation tool, and the software which I am
installing will
only work if the regional settings are correct.

Cheers
Chris.
 
S

Stanimir Stoyanov

theinvisibleGhost said:
Is there a method of determining which format the short date format is
stored in for the current user on the machine using .NET?

Hello Chris,

You can make use of the CultureInfo.CurrentCulture object for regional
setting checks. The following code excerpt will output the short date format
for the current user (dd/MM/yyyy for en-GB):

using System.Globalization;
.....
CultureInfo ci = CultureInfo.CurrentCulture;
Console.WriteLine(ci.DateTimeFormat.ShortDatePattern);

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org
 
M

Michael Nemtsev, MVP

Hello theinvisibleGhost,

Detect the culture first, and that use DateTimeFormatInfo class to get the
time format
http://msdn2.microsoft.com/en-us/library/system.globalization.datetimeformatinfo(VS.71).aspx

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


t> Is there a method of determining which format the short date format
t> is stored in for the current user on the machine using .NET?
t>
 
T

theinvisibleGhost

Hello theinvisibleGhost,

Detect the culture first, and that use DateTimeFormatInfo class to get the
time formathttp://msdn2.microsoft.com/en-us/library/system.globalization.datetim...

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

t> Is there a method of determining which format the short date format
t> is stored in for the current user on the machine using .NET?
t>

Cheers, I've got it to determine the date format, which is a great
start.
If anyone has any ideas, how I can set the format to be this way
I'd be quite interested!

Thanks
Chris
 
T

theinvisibleGhost

OK I found a way of doing this using the Windows API
Code Sample:
___________________________________________
[DllImport("kernel32.dll")]
private static extern uint GetUserDefaultLCID();
[DllImport("kernel32.dll")]
static extern bool SetLocaleInfo(uint Locale, uint LCType,
string lpLCData);
public const int LOCALE_SSHORTDATE = 0x1F;
public const int LOCALE_SDATE = 0x1D;

/// <summary>
/// Sets the short date.
/// </summary>
/// <param name="strShortDate">The STR short date.</param>
public static void SetShortDate(string strShortDate)
{
uint lngLCID;
lngLCID = GetUserDefaultLCID();
SetLocaleInfo(lngLCID, LOCALE_SSHORTDATE, strShortDate);
SetLocaleInfo(lngLCID, LOCALE_SDATE, "/");
}

SetShortDate ("dd/MMM/yyyy");
_____________________________________
I adapted this from this thread here
http://www.codeguru.com/forum/showthread.php?t=16807
 
D

Doug Semler

theinvisibleGhost said:
Is there a method of determining which format the short date format is
stored in for the current user on the machine using .NET?

If it is not in UK format I ideally need to be able to set it to UK
format as well.

I'm building an installation tool, and the software which I am
installing will
only work if the regional settings are correct.


Your software actually requires a rigid format for dates? You force someone
in the US that is using your software to set their date format to something
unfamiliar to them?

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
T

theinvisibleGhost

Your software actually requires a rigid format for dates? You force someone
in the US that is using your software to set their date format to something
unfamiliar to them?

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

The software actually comes from the US.
It's a client server system. The server has a setting to either be
in US or UK mode. Which ever it is the clients must be set
to be the same. Not ideal, but it works.
The clients are only ever and due to certain medical
legal requirements will ever be in the same country
as the server. I think it's got to a point where if it aint
broke it won't be fixed!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top