How to format date (1 Viewer)

prasadgov

Member
Local time
Today, 12:11
Joined
Oct 12, 2021
Messages
44
TIA,

I need to format this date similar to 7/24/2018 8:09:45 AM

The field, field4 value is PREP--20240626072323
I am using Prep_DateTime: IIf([Field2]="98C",Format(Right([Field4],14),"dd/mm/yyyy hh:nn:ss")) but it is incorrect.
What is the format for General date?
 

plog

Banishment Pending
Local time
Today, 11:11
Joined
May 11, 2011
Messages
11,724
Format() requires the correct input to provide the correct output. You are giving it a string and hoping it can parse it and make it a date for you. It needs help.


DateValue() can convert strings into Dates:


And TimeValue() can convert strings into Time:


You are going to need to extract your field value further and use both of those functions with their own Format() to produce what you want.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 17:11
Joined
Sep 12, 2006
Messages
15,796
But it is awkward to define a date correctly when there is a time involved.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 17:11
Joined
Feb 19, 2013
Messages
16,775
I need to format this date similar to 7/24/2018 8:09:45 AM
you need a format month-day-year and AM/PM time format

you tried
"dd/mm/yyyy hh:nn:ss"
which is day-month-year and 24 hour time format

So which is it you actually want?

This code will parse a 14 char string to produce a date format
Rich (BB code):
Function formatDate(s As String) As String
'expects a string in the form yyyymmddhhnnss (14 chars)
Dim i As Integer
Dim d As String
    
    For i = 1 To Len(s)
        d = d & Mid(s, i, 1)
        Select Case i
        
            Case 4, 6 'date separator, insert a -
                 d = d & "-"
                
            Case 8 'date time separator, insert a space
                 d = d & " "
                
            Case 10, 12 'time separator, insert a :
                 d = d & ":"
        
        End Select
                    
    Next i

    'formatDate = Format(d, "m/d/yyyy hh:nn:ss AM/PM")
    'or
    formatDate = Format(d, "dd/mm/yyyy hh:nn:ss")

End Function

in your query it might be called with something like

Prep_DateTime: IIf([Field2]="98C",FormatDate(Right([Field4],14))
 

Users who are viewing this thread

Top Bottom