Funció FormatDateTime [VBA]

Aplica un format de data i/o d'hora a una expressió de data i retorna el resultat com a cadena de caràcters.

warning

Aquesta funció o constant s'activa amb l'expressió Option VBASupport 1 plaçada en un mòdul abans del codi executable.


Sintaxi:


        FormatDateTime (DateExpression as Date [, NamedFormat as Integer])
    

Valor de retorn:

String

Paràmetres :

DateExpression: The date expression to be formatted.

NamedFormat: An optional vbDateTimeFormat enumeration specifying the format that is to be applied to the date and time expression. If omitted, the value vbGeneralDate is used.

Formats de data i hora (enumeració vbDateTimeFormat)

Constant amb nom

Valor

Descripció

vbGeneralDate

0

Displays a date and/or time as defined in your system's General Date setting. If a date only, no time is displayed; If a time only, no date is displayed.

vbLongDate

1

Display a date using the long date format specified in your computer's regional settings.

vbShortDate

2

Display a date using the short date format specified in your computer's regional settings.

vbLongTime

3

Displays a time as defined in your system's Long Time settings.

vbShortTime

4

Mostra l'hora utilitzant el format de 24 hores (hh:mm).


Codis d'error

13 Els tipus de dades no coincideixen

Exemple :


        REM  *****  BASIC  *****
        Option VBASupport 1
        Sub DateFormat
         Dim d as Date
         d = ("1958-01-29 00:25")
         msgbox("General date format : " & FormatDateTime(d))
         msgbox("Long date format : " & FormatDateTime(d,vbLongDate))
         msgbox("Format de data curt : " & FormatDateTime(d,vbShortDate))
         msgbox("Format d'hora llarg : " & FormatDateTime(d,3))
         msgbox("Format d'hora curt : " & FormatDateTime(d,vbShortTime))
        End Sub
    

Ens cal la vostra ajuda!