I've just entered a date of "03/06/2021". Is that a correct format? Yes?
Well, guess what. I entered that to represent June 3rd, 2021. In other words, I entered "DD/mm/yyyy". But could you tell? No? Then neither can your code.
Maybe you need to use more than one sentence to explain your situation clearly.
I want to check whether Short Date Format in Control Panel -> Region Settings is MM/dd/yyyy.
I am trying below code
Code:
Private 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
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_SSHORTDATE = &H1F ' short date format string
Private Sub Form_Load()
Dim strLocale As String
Dim lngRet As Long
Dim strMsg As String
'Get short date format
strLocale = Space(255)
lngRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, strLocale, Len(strLocale))
strLocale = Left(strLocale, lngRet - 1)
strMsg = "Short Date Format: " & strLocale
End Sub
How elaborate do you need to get? Are you really just trying to prevent times along with/instead of dates?
How about just using the IsDate() function?
Or this one (not fully tested) takes it further:
Code:
Option Explicit
'Note: This Class has "Attribute VB_PredeclaredId = True" so it provides a default
' global instance.
Private ShortDatePartLens() As Integer
Private ShortDateParts() As String
Private mDelim As String
Private mPattern As String
Public Property Get Delim() As String
Delim = mDelim
End Property
Private Property Let Delim(ByVal RHS As String)
mDelim = RHS
End Property
Public Property Get Pattern() As String
Pattern = mPattern
End Property
Private Property Let Pattern(ByVal RHS As String)
mPattern = RHS
End Property
Public Function Validate(ByRef DateOnlyString As String) As Boolean
Dim Parts() As String
Dim I As Integer
Dim IntPart As Integer
Parts = Split(Replace$(DateOnlyString, " ", ""), Delim)
If UBound(Parts) <> UBound(ShortDatePartLens) Then Exit Function
For I = 0 To UBound(ShortDatePartLens)
If Len(Parts(I)) > ShortDatePartLens(I) Then Exit Function
If Parts(I) Like "*[!0-9]*" Then Exit Function
Select Case UCase$(Left$(ShortDateParts(I), 1))
Case "D"
IntPart = CInt(Parts(I))
If 1 > IntPart Or IntPart > 31 Then Exit Function
Case "M"
IntPart = CInt(Parts(I))
If 1 > IntPart Or IntPart > 12 Then Exit Function
Case "Y"
Select Case Len(Parts(I))
Case 1, 3 'Change to "Is < 4" to deny validity of 2-digit years.
Exit Function
End Select
End Select
Next
Validate = True
End Function
Private Sub Class_Initialize()
Dim I As Integer
Delim = GetAsString(LOCALE_SDATE)
Pattern = GetAsString(LOCALE_SSHORTDATE)
ShortDateParts = Split(Pattern, Delim)
ReDim ShortDatePartLens(UBound(ShortDateParts))
For I = 0 To UBound(ShortDateParts)
ShortDatePartLens(I) = Len(ShortDateParts(I))
If ShortDatePartLens(I) = 1 Then ShortDatePartLens(I) = 2
Next
End Sub
I want to check whether Short Date Format in Control Panel -> Region Settings is MM/dd/yyyy.
I am trying below code
Code:
Private 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
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_SSHORTDATE = &H1F ' short date format string
Private Sub Form_Load()
Dim strLocale As String
Dim lngRet As Long
Dim strMsg As String
'Get short date format
strLocale = Space(255)
lngRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, strLocale, Len(strLocale))
strLocale = Left(strLocale, lngRet - 1)
strMsg = "Short Date Format: " & strLocale
End Sub
Thanks
It works for me.
Code:
Option Explicit
Private 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
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_SSHORTDATE = &H1F ' short date format string
Private Sub Form_Load()
Dim strLocale As String
Dim lngRet As Long
Dim strMsg As String
'Get short date format
strLocale = Space(255)
lngRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, strLocale, Len(strLocale))
strLocale = Left(strLocale, lngRet - 1)
If strLocale = "MM/dd/yyyy" Then
MsgBox "It is."
Else
MsgBox "It is not."
End If
End Sub