Restituisce un'espressione formattato come data o ora.
Sintassi
FormatDateTime(Date[,NamedFormat])
Argomenti:
Nota:
Impostazioni: per l'argomento NamedFormat è possibile utilizzare le seguenti impostazioni:
vbGeneralDate: per ottenere la data nel formato di data generale, passare il valore 0.
Visualizza la data e/o l'ora. Se esiste una parte della data, la visualizza come data breve. Se esiste una parte dell'ora, la visualizza come ora estesa. Se presenti, vengono visualizzate entrambe le parti.
Esempio 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: per ottenere la data estesa, passare il valore 1.
Visualizza una data utilizzando il formato di data estesa specificato nelle impostazioni nazionali del computer.
Nota:
Nel formato di data estesa il contenuto viene visualizzato solo nel formato inglese; l'analisi dei giorni feriali non è supportata.Esempio 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: per ottenere la data breve, passare il valore 2.
Visualizza una data utilizzando il formato di data breve specificato nelle impostazioni nazionali del computer.
Esempio 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: per ottenere l'ora nel formato di ora esteso, passare il valore 3.
Visualizza un'ora utilizzando il formato di ora breve specificato nelle impostazioni nazionali del computer.
Nota:
Nel formato di ora estesa il contenuto viene visualizzato nel formato inglese, ad esempio viene visualizzato AM o PM.Esempio 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: per ottenere l'ora nel formato di ora breve, passare il valore 4.
Visualizza un'ora utilizzando il formato a 24 ore (hh:mm).
Esempio 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