DateAndTime.DatePart Metodo

Definizione

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

Overload

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

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

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

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

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

Source:
DateAndTime.vb
Source:
DateAndTime.vb
Source:
DateAndTime.vb

Restituisce un intero contenente il componente specificato di un determinato valore 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

Obbligatorio. Valore dell'enumerazione DateInterval o espressione String che rappresenta la parte del valore di data/ora da restituire.

DateValue
DateTime

Obbligatorio. Valore Date da valutare.

FirstDayOfWeekValue
FirstDayOfWeek

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

FirstWeekOfYearValue
FirstWeekOfYear

facoltativo. Valore scelto dall'enumerazione FirstWeekOfYear che specifica il primo giorno dell'anno. Se omesso, viene utilizzato Jan1.

Restituisce

Intero contenente il componente specificato di un determinato valore Date.

Eccezioni

Interval non è valido.

DateValue non può essere assegnato forzatamente a un valore Date.

Esempio

Questo esempio accetta una data e, usando la funzione, visualizza il trimestre dell'anno DatePart 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 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, il valore restituito è coerente con i valori dell'enumerazione IntervalFirstDayOfWeek . Se si sceglie DateInterval.WeekOfYear, DatePart usa le classi e CultureInfo dello System.Globalization spazio dei nomi per determinare le Calendar 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 nel 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 string Parte del valore 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 Month
DateInterval.Quarter q Quarter
DateInterval.Second s Second
DateInterval.Weekday w Giorno della settimana (da 1 a 7).
DateInterval.WeekOfYear ww Settimana dell'anno (da 1 a 53).
DateInterval.Year aaaa Year

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

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

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

Valore di enumerazione valore Descrizione
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 completa nel nuovo anno

Vedi anche

Si applica a

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

Source:
DateAndTime.vb
Source:
DateAndTime.vb
Source:
DateAndTime.vb

Restituisce un valore contenente il componente specificato di un determinato valore 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);
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

Obbligatorio. Valore dell'enumerazione DateInterval o espressione String che rappresenta la parte del valore di data/ora da restituire.

DateValue
Object

Obbligatorio. Valore Date da valutare.

DayOfWeek
FirstDayOfWeek

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

WeekOfYear
FirstWeekOfYear

facoltativo. Valore scelto dall'enumerazione FirstWeekOfYear che specifica il primo giorno dell'anno. Se omesso, viene utilizzato Jan1.

Restituisce

Intero contenente il componente specificato di un determinato valore Date.

Eccezioni

Interval non è valido.

DateValue non può essere assegnato forzatamente a un valore Date.

Esempio

Questo esempio accetta una data e, usando la funzione, visualizza il trimestre dell'anno DatePart 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 utilizzare DatePart per calcolare il giorno della settimana o l'ora corrente.

Se si sceglie DateInterval.Weekday 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 spazio dei System.Globalization 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 string 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 Month
DateInterval.Quarter q Quarter
DateInterval.Second s Second
DateInterval.Weekday w Giorno della settimana (da 1 a 7).
DateInterval.WeekOfYear ww Settimana dell'anno (da 1 a 53).
DateInterval.Year aaaa Year

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

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

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

Valore di enumerazione valore Descrizione
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 completa nel nuovo anno

Vedi anche

Si applica a