Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Format from Control Format 1

Status
Not open for further replies.

TMoney216

Programmer
Mar 1, 2002
11
0
0
US
Hi All,

What is the easiest way to get the current date format from the Regional Settings in the Control Panel. I'm working with a datagrid and need to know prior to working with a column containing a date, what the grid will date format will be.....I guess that's based on the Regional Settings, I believe.... Any help would be greatly appreciated...thanks in advance!!!
 
VB will automatically pick up the default date format from regional settings. So If you want to figure out what the PC is using try this.

dim strDate as string
dim strDay as string
dim intPointer as integer

strDate = Date() ' Get the date in string format
strDay = Format(Date(), "NN") ' This wil get the day of the month.

'Then find the placement of strDay in strDate
intPointer = InStr(strDate, strDay, len(strDay))
If intPointer = 1 then
'Day is first
ElseIf intPointer = 3
'Month is first
End If
Craig, mailto:sander@cogeco.ca
"Procrastination is the art of keeping up with yesterday."
I hope my post was helpful!!!
 
The above answer will not always work for many reasons:
1. Date() could just happen to be 02/02/02, or similar - then what?
2. Format(Date(), "NN") gives you the Minutes and not the Days.
3. InStr(strDate, strDay, len(strDay)) returns what?

4. strDate needs also to be formated before comparing (either as 1/2/02 or 01/02/02 and strDay then accordingly (either as 1 or 01

5. The date could be in international format (with a dot as a date seperator - day.month.year

'Better is to use the API function: GetLocaleInfo
'If you look up the additional constants then you can use this function to get all the other country settings found in the control panel, and with additional API function, you can set the system to something different

'Put the following in the declarations section of a Module:
'========================
Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Public Const LCID

Public Const LOCALE_SSHORTDATE As Long = &H1F 'short date format string
Public Const LOCALE_SLONGDATE As Long = &H20 'long date format string

Public Declare Function GetLocaleInfo Lib "kernel32" _
Alias "GetLocaleInfoA" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String, _
ByVal cchData As Long) As Long
'========================

'Then, in the same module create the following Function:

'========================
Public Function Fp_GetUserLocaleInfo(ByVal dwLocaleID As Long, _
ByVal dwLCType As Long) As String

Dim sReturn As String
Dim r As Long

'call the function passing the Locale type
'variable to retrieve the required size of
'the string buffer needed
r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))

If r Then 'if successful..
sReturn = Space$(r) 'pad the buffer with spaces

'and call again passing the buffer
r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))

'if successful (r > 0)
If r Then

'r holds the size of the string
'including the terminating null
Fp_GetUserLocaleInfo = Left$(sReturn, r - 1)
End If
End If

End Function
'========================

'Call it as in following, changing TxtBx(Gc0).Text
'TxtBx(Gc1).Text to your text box or variable
'names accordingly:

'========================
Public Sub GetSysDateFormat()

LCID = GetSystemDefaultLCID()

'Show the system set Long date format string
TxtBx(Gc0).Text = modSystemData.Fp_GetUserLocaleInfo LCID, LOCALE_SLONGDATE)

'Show the system set Short date format string
TxtBx(Gc1).Text = modSystemData.Fp_GetUserLocaleInfo(LCID, LOCALE_SSHORTDATE)

End Sub
'========================

'PS. If this doesn't work then let me know as I made
'some adjustments here with-out testing them.
 
Hey Thanks.......that's exactly what I was looking for!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top