Link to home
Start Free TrialLog in
Avatar of Mikal613
Mikal613Flag for United States of America

asked on

Get format of a date?

Can i retrieve the format of the Date?

For example

if i have 5/25/05

It should return m/dd/yy

if i have February 12, 2006

it should return
mmmm dd,yyyy (or something like that)
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dates are stored in memory the same way, regardless of how it is displayed, so you should be able to use

Format(YourDate, "Short Date") for m/d/yy                    '06/01/2006

Format(YourDate, "Long Date") for mmmm dd,yyyy        'Thursday, June 01, 2006


Hope this helps.
-JIm
Avatar of MilanKM
MilanKM

Try this:

Private Sub Form_Load()
    MsgBox Format(Date, "mmmm dd,yyyy")
End Sub

Thanks
MilanKM
Avatar of Mikal613

ASKER

Public function GetDateFormat(DT as Date) as String
    'Something here
End Function


Msgbox GetDAteFormat("5/23/06")

Should retutn m/dd/yy

Msgbox GetDAteFormat("05/23/06")

Should retutn mm/dd/yy

Msgbox GetDAteFormat("05/23/2006")

Should retutn mm/dd/yyyy

Isnt there a System function or API
Mikal613

Can you explain how the answer you accepted solved your problem?  Just curious.
Sorry that juyst gets the local Date separator
I wasn't expecting my comment to be accepted as an answer - I was seeking clarification before launching into a few possible algorithms for the most common formats.
If you hit the wrong key, you could get a moderator to unwind it and try again.
no I didn't make a mistake. if you have an answer ill be more then glad to take it. your answer  makes sense so I gave u the points.
It isn't comprehensive, but you could start with this:

Function GetDateFormat(strDate As String) As String
    Dim dt As Date
    Dim i As Integer
    Dim l As Integer
    Dim m As Integer
    Dim wd As Integer
    Dim parts() As String
    Dim Components() As String
   
    If Not IsDate(strDate) Then
        Exit Function
    End If
    dt = CDate(strDate)
    parts = Split(strDate, "/")
    Select Case UBound(parts)
        Case 1, Is > 2
            Exit Function
        Case 2
            For i = 0 To 2
                If Len(parts(i)) = 2 Then
                    l = l + 1
                End If
            Next i
            If l = 3 Then
                If Format$(dt, "dd/mm/yy") = strDate Then
                    GetDateFormat = "dd/mm/yy"
                End If
                If Format$(dt, "mm/dd/yy") = strDate Then
                    GetDateFormat = "mm/dd/yy"
                End If
                If Format$(dt, "yy/mm/dd") = strDate Then
                    GetDateFormat = "yy/mm/dd"
                End If
            End If
            Exit Function
    End Select
    parts = Split(strDate, " ")
    ReDim Components(UBound(parts))
        For i = 0 To UBound(parts)
            For m = 1 To 12
                If parts(i) = Format$(DateSerial(2000, m, 1), "mmmm") Then
                    Components(i) = "mmmm"
                End If
                If parts(i) = Format$(DateSerial(2000, m, 1), "mmm") Then
                    Components(i) = "mmm"
                End If
            Next m
            For wd = 1 To 7
                If parts(i) = Format$(DateSerial(2000, 1, wd), "ddd") Then
                    Components(i) = "ddd"
                End If
                If parts(i) = Format$(DateSerial(2000, 1, wd), "dddd") Then
                    Components(i) = "dddd"
                End If
            Next wd
            If IsNumeric(parts(i)) Then
                Select Case Len(parts(i))
                    Case 4
                        Components(i) = "yyyy"
                    Case 1, 2
                        Components(i) = "d"
                End Select
            End If
        Next i
        For i = 0 To (UBound(parts))
            If Components(i) <> "" Then
                parts(i) = Components(i)
            End If
        Next i
        GetDateFormat = Join(parts, " ")
End Function