DateAndTime.DatePart Metodo

Definizione

Restituisce un valore intero contenente il componente specificato di un valore specificato Date .

Overload

Nome Descrizione
DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

Restituisce un valore intero contenente il componente specificato di un valore specificato Date .

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

Restituisce un valore contenente il componente specificato di un valore specificato Date .

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

Restituisce un valore intero contenente il componente specificato di un valore specificato Date .

public static int DatePart(Microsoft.VisualBasic.DateInterval Interval, DateTime DateValue, Microsoft.VisualBasic.FirstDayOfWeek FirstDayOfWeekValue = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Microsoft.VisualBasic.FirstWeekOfYear FirstWeekOfYearValue = Microsoft.VisualBasic.FirstWeekOfYear.Jan1);
static member DatePart : Microsoft.VisualBasic.DateInterval * DateTime * Microsoft.VisualBasic.FirstDayOfWeek * Microsoft.VisualBasic.FirstWeekOfYear -> int
Public Function DatePart (Interval As DateInterval, DateValue As DateTime, Optional FirstDayOfWeekValue As FirstDayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Optional FirstWeekOfYearValue As FirstWeekOfYear = Microsoft.VisualBasic.FirstWeekOfYear.Jan1) As Integer

Parametri

Interval
DateInterval

Required. Valore DateInterval di enumerazione o espressione stringa che rappresenta la parte del valore di data/ora da restituire.

DateValue
DateTime

Required. Valore Date da valutare.

FirstDayOfWeekValue
FirstDayOfWeek

Optional. Valore scelto dall'enumerazione FirstDayOfWeek che specifica il primo giorno della settimana. Se omesso, viene utilizzato Sunday.

FirstWeekOfYearValue
FirstWeekOfYear

Optional. Valore scelto dall'enumerazione FirstWeekOfYear che specifica la prima settimana dell'anno. Se omesso, viene utilizzato Jan1.

Valori restituiti

Valore intero contenente il componente specificato di un valore specificato Date .

Eccezioni

Interval non è valido.

DateValue non è coercibile a Date.

Esempio

In questo esempio viene accettata una data e, usando la DatePart funzione , viene visualizzato il trimestre dell'anno in cui si verifica.

Dim DateString, Msg As String
Dim ActualDate As Date
' Enter February 12, 2008, or 2/12/2008.
DateString = InputBox("Enter a date:")
ActualDate = CDate(DateString)

' The first two examples use enumeration values for the interval.
Msg = "Quarter: " & DatePart(DateInterval.Quarter, ActualDate)
' The quarter is 1.
MsgBox(Msg)
Msg = "The day of the month: " & DatePart(DateInterval.Day, ActualDate)
' The day of the month is 12.
MsgBox(Msg)

' The next two examples use string values for the interval parameter.
Msg = "The week of the year: " & DatePart("ww", ActualDate)
' The week of the year is 7.
MsgBox(Msg)
Msg = "The day of the week: " & DatePart("w", ActualDate)
' The day of the week is 3 (Tuesday).
MsgBox(Msg)

Commenti

È possibile usare la DatePart funzione per valutare un valore di data/ora e restituire un componente specifico. Ad esempio, è possibile usare DatePart per calcolare il giorno della settimana o l'ora corrente.

Se si sceglie DateInterval.Weekday per l'argomento Interval , il valore restituito è coerente con i valori dell'enumerazione FirstDayOfWeek . Se si sceglie DateInterval.WeekOfYear, DatePart usa le Calendar classi e CultureInfo dello System.Globalization spazio dei nomi per determinare le impostazioni correnti.

L'argomento FirstDayOfWeekValue influisce sui calcoli che usano le DateInterval.Weekday impostazioni e DateInterval.WeekOfYearInterval . L'argomento FirstWeekOfYearValue influisce sui calcoli che specificano DateInterval.WeekOfYear per Interval.

Poiché ogni Date valore è supportato da una DateTime struttura, i relativi metodi offrono opzioni aggiuntive per il recupero di parti di data/ora. Ad esempio, è possibile ottenere l'intero valore di data di una Date variabile, con il valore di ora impostato su mezzanotte, come indicato di seguito:

Dim CurrDatTim As Date = Now   ' Current date and time.
Dim LastMidnight As Date = CurrDatTim.Date   ' At midnight.

L'argomento Interval può avere una delle impostazioni seguenti.

Valore di enumerazione Stringa Parte del valore di data/ora da restituire
DateInterval.Day d Giorno del mese (da 1 a 31)
DateInterval.DayOfYear Y Giorno dell'anno (da 1 a 366)
DateInterval.Hour h Ora
DateInterval.Minute n Minuto
DateInterval.Month m Mese
DateInterval.Quarter q Trimestre
DateInterval.Second s Seconda
DateInterval.Weekday w Giorno della settimana (da 1 a 7)
DateInterval.WeekOfYear ww Settimana dell'anno (da 1 a 53)
DateInterval.Year aaaa Anno

L'argomento FirstDayOfWeekValue può avere una delle impostazioni seguenti.

Valore di enumerazione Value Description
FirstDayOfWeek.System 0 Primo giorno della settimana specificato nelle impostazioni di sistema
FirstDayOfWeek.Sunday 1 Domenica (impostazione predefinita)
FirstDayOfWeek.Monday 2 Monday (conforme allo standard ISO 8601, sezione 3.17)
FirstDayOfWeek.Tuesday 3 Martedì
FirstDayOfWeek.Wednesday 4 Mercoledì
FirstDayOfWeek.Thursday 5 Giovedì
FirstDayOfWeek.Friday 6 Venerdì
FirstDayOfWeek.Saturday 7 Sabato

L'argomento FirstWeekOfYearValue può avere una delle impostazioni seguenti.

Valore di enumerazione Value Description
FirstWeekOfYear.System 0 Prima settimana dell'anno specificata nelle impostazioni di sistema
FirstWeekOfYear.Jan1 1 Settimana in cui si verifica il 1° gennaio (impostazione predefinita)
FirstWeekOfYear.FirstFourDays 2 Settimana con almeno quattro giorni nel nuovo anno (conforme allo standard ISO 8601, sezione 3.17)
FirstWeekOfYear.FirstFullWeek 3 Prima settimana intera nel nuovo anno

Vedi anche

Si applica a

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

Restituisce un valore contenente il componente specificato di un valore specificato Date .

public static int DatePart(string Interval, object DateValue, Microsoft.VisualBasic.FirstDayOfWeek DayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Microsoft.VisualBasic.FirstWeekOfYear WeekOfYear = Microsoft.VisualBasic.FirstWeekOfYear.Jan1);
static member DatePart : string * obj * Microsoft.VisualBasic.FirstDayOfWeek * Microsoft.VisualBasic.FirstWeekOfYear -> int
Public Function DatePart (Interval As String, DateValue As Object, Optional DayOfWeek As FirstDayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Optional WeekOfYear As FirstWeekOfYear = Microsoft.VisualBasic.FirstWeekOfYear.Jan1) As Integer

Parametri

Interval
String

Required. Valore DateInterval di enumerazione o espressione stringa che rappresenta la parte del valore di data/ora da restituire.

DateValue
Object

Required. Valore Date da valutare.

DayOfWeek
FirstDayOfWeek

Optional. Valore scelto dall'enumerazione FirstDayOfWeek che specifica il primo giorno della settimana. Se omesso, viene utilizzato Sunday.

WeekOfYear
FirstWeekOfYear

Optional. Valore scelto dall'enumerazione FirstWeekOfYear che specifica la prima settimana dell'anno. Se omesso, viene utilizzato Jan1.

Valori restituiti

Valore intero contenente il componente specificato di un valore specificato Date .

Eccezioni

Interval non è valido.

DateValue non è coercibile a Date.

Esempio

In questo esempio viene accettata una data e, usando la DatePart funzione , viene visualizzato il trimestre dell'anno in cui si verifica.

Dim DateString, Msg As String
Dim ActualDate As Date
' Enter February 12, 2008, or 2/12/2008.
DateString = InputBox("Enter a date:")
ActualDate = CDate(DateString)

' The first two examples use enumeration values for the interval.
Msg = "Quarter: " & DatePart(DateInterval.Quarter, ActualDate)
' The quarter is 1.
MsgBox(Msg)
Msg = "The day of the month: " & DatePart(DateInterval.Day, ActualDate)
' The day of the month is 12.
MsgBox(Msg)

' The next two examples use string values for the interval parameter.
Msg = "The week of the year: " & DatePart("ww", ActualDate)
' The week of the year is 7.
MsgBox(Msg)
Msg = "The day of the week: " & DatePart("w", ActualDate)
' The day of the week is 3 (Tuesday).
MsgBox(Msg)

Commenti

È possibile usare la DatePart funzione per valutare un valore di data/ora e restituire un componente specifico. Ad esempio, è possibile usare DatePart per calcolare il giorno della settimana o l'ora corrente.

Se si sceglie DateInterval.Weekday per l'argomento Interval , il valore restituito è coerente con i valori dell'enumerazione FirstDayOfWeek . Se si sceglie DateInterval.WeekOfYear, DatePart usa le Calendar classi e CultureInfo dello System.Globalization spazio dei nomi per determinare le impostazioni correnti.

L'argomento FirstDayOfWeekValue influisce sui calcoli che usano le DateInterval.Weekday impostazioni e DateInterval.WeekOfYearInterval . L'argomento FirstWeekOfYearValue influisce sui calcoli che specificano DateInterval.WeekOfYear per Interval.

Poiché ogni Date valore è supportato da una DateTime struttura, i relativi metodi offrono opzioni aggiuntive per il recupero di parti di data/ora. Ad esempio, è possibile ottenere l'intero valore di data di una Date variabile, con il valore di ora impostato su mezzanotte, come indicato di seguito:

Dim CurrDatTim As Date = Now   ' Current date and time.
Dim LastMidnight As Date = CurrDatTim.Date   ' At midnight.

L'argomento Interval può avere una delle impostazioni seguenti.

Valore di enumerazione Stringa Parte del valore di data/ora da restituire
DateInterval.Day d Giorno del mese (da 1 a 31)
DateInterval.DayOfYear Y Giorno dell'anno (da 1 a 366)
DateInterval.Hour h Ora
DateInterval.Minute n Minuto
DateInterval.Month m Mese
DateInterval.Quarter q Trimestre
DateInterval.Second s Seconda
DateInterval.Weekday w Giorno della settimana (da 1 a 7)
DateInterval.WeekOfYear ww Settimana dell'anno (da 1 a 53)
DateInterval.Year aaaa Anno

L'argomento FirstDayOfWeekValue può avere una delle impostazioni seguenti.

Valore di enumerazione Value Description
FirstDayOfWeek.System 0 Primo giorno della settimana specificato nelle impostazioni di sistema
FirstDayOfWeek.Sunday 1 Domenica (impostazione predefinita)
FirstDayOfWeek.Monday 2 Monday (conforme allo standard ISO 8601, sezione 3.17)
FirstDayOfWeek.Tuesday 3 Martedì
FirstDayOfWeek.Wednesday 4 Mercoledì
FirstDayOfWeek.Thursday 5 Giovedì
FirstDayOfWeek.Friday 6 Venerdì
FirstDayOfWeek.Saturday 7 Sabato

L'argomento FirstWeekOfYearValue può avere una delle impostazioni seguenti.

Valore di enumerazione Value Description
FirstWeekOfYear.System 0 Prima settimana dell'anno specificata nelle impostazioni di sistema
FirstWeekOfYear.Jan1 1 Settimana in cui si verifica il 1° gennaio (impostazione predefinita)
FirstWeekOfYear.FirstFourDays 2 Settimana con almeno quattro giorni nel nuovo anno (conforme allo standard ISO 8601, sezione 3.17)
FirstWeekOfYear.FirstFullWeek 3 Prima settimana intera nel nuovo anno

Vedi anche

Si applica a