• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 903
  • Last Modified:

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)
0
Mikal613
Asked:
Mikal613
  • 4
  • 3
  • 2
  • +1
1 Solution
 
GrahamSkanCommented:
The intended date is frequently ambiguous, e.g what would you expect 10/11/06 to return: dd/mm/yy, mm/dd/yy or even yy/mm/dd?
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
0
 
MilanKMCommented:
Try this:

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

Thanks
MilanKM
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mikal613Author Commented:
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

0
 
Mikal613Author Commented:
Isnt there a System function or API
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Mikal613

Can you explain how the answer you accepted solved your problem?  Just curious.
0
 
Mikal613Author Commented:
Sorry that juyst gets the local Date separator
0
 
GrahamSkanCommented:
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.
0
 
Mikal613Author Commented:
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.
0
 
GrahamSkanCommented:
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now