FormatDateTime-Funktionen

Gibt einen Ausdruck zurück, der als Datum oder Uhrzeit formatiert ist.

Syntax

FormatDateTime(Date[,NamedFormat])

Argumente:

  • Date: Erforderlich. Datumsausdruck, der formatiert werden soll.

    Hinweis:

    • Wenn eine Zeichenfolge als Eingabe angegeben wird, müssen Begrenzungszeichen, die in Eingabedatum bzw. -uhrzeit verwendet werden, mit den systemspezifischen Begrenzungszeichen des jeweiligen Gebietsschemas übereinstimmen. Die hier abgebildeten Sonderzeichen Unzulässige Zeichen werden nicht unterstützt.
    • Die Anzahl der Tage darf nicht als Eingabeargument angegeben werden. Sie wird nicht unterstützt.
    • Datum, Monat und Jahr müssen im vollständigen Format angegeben werden. Beispiel: 08/02/2024, Abkürzungen wie 8/2/24 werden nicht unterstützt.
  • NamedFormat: Optional. Numerischer Wert, der das verwendete Datums- und Uhrzeitformat angibt. Wenn keine Angabe gemacht wird, wird "vbGeneralDate" verwendet. Informationen zu Werten finden Sie im Abschnitt "Einstellungen".

Einstellungen: Das Argument "NamedFormat" weist die folgenden Einstellungen auf:

vbGeneralDate: Um das Datum im allgemeinen Datumsformat abzurufen, übergeben Sie 0 als Wert für "Value".

Zeigen Sie ein Datum und/oder eine Uhrzeit an. Wenn ein Datumsteil vorhanden ist, zeigen Sie ihn als kurzes Datum an. Wenn ein Zeitteil vorhanden ist, zeigen Sie ihn im langen Format an. Wenn vorhanden, werden beide Teile angezeigt.

Beispiel 1:

Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,0)    
'Output: 12/19/2024 12:00:00 AM

d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,0)            
'Output: - 12/19/2024 02:30:00 PM

d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,0)    
'Output: - 12/19/2024 02:30:10 PM

d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,0)
'Output: - 12/19/2024 02:30:00 PM

d="12/19/2024 2pm"
mytime = FormatDateTime(d,0)    
Output: - 12/19/2024 02:00:00 PM

d="2 pm"
mytime = FormatDateTime(d,0)
'Output: - 12/30/1899 02:00:00 PM

d="12-19-2024"
mytime = FormatDateTime(d,0)
'Output: - ERROR as delimiter should be same as locale’s delimiter

vbLongDate: So rufen Sie "Long Date Pass Value 1" ab.

Zeigen Sie ein Datum mit dem langen Datumsformat an, das in den regionalen Einstellungen Ihres Computers angegeben ist.

Hinweis:

Das lange Datum zeigt den Inhalt nur in englischen Zeichen an, und das Parsen von Wochentagen wird nicht unterstützt.

Beispiel 2:

Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,1)    
'Output: 12/19/2024 12:00:00 AM

d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,1)            
'Output: - 12/19/2024 02:30:00 PM

d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,1)    
'Output: - 12/19/2024 02:30:10 PM

d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,1)
'Output: - 12/19/2024 02:30:00 PM

d="12/19/2024 2pm"
mytime = FormatDateTime(d,1)    
Output: - 12/19/2024 02:00:00 PM

d="2 pm"
mytime = FormatDateTime(d,1)
'Output: - 12/30/1899 02:00:00 PM

d="12-19-2024"
mytime = FormatDateTime(d,1)
'Output: - ERROR as delimiter should be same as locale’s delimiter

vbShortDate: So rufen Sie "Short Date Pass Value 2" ab.

Zeigen Sie ein Datum mit dem kurzen Datumsformat an, das in den regionalen Einstellungen Ihres Computers angegeben ist.

Beispiel 3:

Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,2)    
'Output: 12/19/2024 12:00:00 AM

d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,2)            
'Output: - 12/19/2024 02:30:00 PM

d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,2)    
'Output: - 12/19/2024 02:30:10 PM

d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,2)
'Output: - 12/19/2024 02:30:00 PM

d="12/19/2024 2pm"
mytime = FormatDateTime(d,2)    
Output: - 12/19/2024 02:00:00 PM

d="2 pm"
mytime = FormatDateTime(d,2)
'Output: - 12/30/1899 02:00:00 PM

d="12-19-2024"
mytime = FormatDateTime(d,2)
'Output: - ERROR as delimiter should be same as locale’s delimiter

vbLongTime: So rufen Sie die Zeit als "Long Time Format Pass Value 3" ab.

Zeigen Sie eine Zeit mit dem Zeitformat an, das in den regionalen Einstellungen Ihres Computers angegeben ist.

Hinweis:

"LongTime" zeigt den Inhalt nur in englischen Zeichen wie AM oder PM an.

Beispiel 4:

Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,3)    
'Output: 12/19/2024 12:00:00 AM

d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,3)            
'Output: - 12/19/2024 02:30:00 PM

d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,3)    
'Output: - 12/19/2024 02:30:10 PM

d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,3)
'Output: - 12/19/2024 02:30:00 PM

d="12/19/2024 2pm"
mytime = FormatDateTime(d,3)    
Output: - 12/19/2024 02:00:00 PM

d="2 pm"
mytime = FormatDateTime(d,3)
'Output: - 12/30/1899 02:00:00 PM

d="12-19-2024"
mytime = FormatDateTime(d,3)
'Output: - ERROR as delimiter should be same as locale’s delimiter

vbShortTime: So rufen Sie die Zeit als "Short Time Format Pass Value 4" ab.

Zeigen Sie eine Zeit im 24-Stunden-Format an (hh:mm).

Beispiel 5:

Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,4)    
'Output: 12/19/2024 12:00:00 AM

d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,4)            
'Output: - 12/19/2024 02:30:00 PM

d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,4)    
'Output: - 12/19/2024 02:30:10 PM

d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,4)
'Output: - 12/19/2024 02:30:00 PM

d="12/19/2024 2pm"
mytime = FormatDateTime(d,4)    
Output: - 12/19/2024 02:00:00 PM

d="2 pm"
mytime = FormatDateTime(d,4)
'Output: - 12/30/1899 02:00:00 PM

d="12-19-2024"
mytime = FormatDateTime(d,4)
'Output: - ERROR as delimiter should be same as locale’s delimiter