Results 1 to 9 of 9

Thread: [RESOLVED] Date Format

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Resolved [RESOLVED] Date Format

    Hi

    I want if Date format is not mm/DD/yyyy then message should appear 'Incorrect Format'.

    Thanks

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    Re: Date Format

    Ok, I'll bite.

    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.

    Good luck.

  3. #3
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,720

    Re: Date Format

    why not add 3 combo-boxes, with month, day, year that the user need to pick.
    or
    add 3 textboxes and 3 labels, with month/day/year specified for each.

    theres always way to do things.

    also, first time I see a user with a shame behavior in the past!

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Date Format

    Hi

    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
    Last edited by Jagjit; Mar 6th, 2021 at 10:36 AM.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Date Format

    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
    Attached Files Attached Files

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,987

    Re: Date Format

    Quote Originally Posted by Jagjit View Post
    Hi

    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

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Date Format

    I misread the question. Better polish my crystal ball.

  8. #8
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Date Format

    Quote Originally Posted by dilettante View Post
    I misread the question. Better polish my crystal ball.
    I don't think the problem is with your balls

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Date Format

    All I could think of was Sprockets:


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width