DateTime.TimeOfDay DateTime.TimeOfDay DateTime.TimeOfDay DateTime.TimeOfDay Property

定義

取得這個執行個體的一天時間。Gets the time of day for this instance.

public:
 property TimeSpan TimeOfDay { TimeSpan get(); };
public TimeSpan TimeOfDay { get; }
member this.TimeOfDay : TimeSpan
Public ReadOnly Property TimeOfDay As TimeSpan

屬性值

時間間隔,表示從午夜以來已經過的當天部分。A time interval that represents the fraction of the day that has elapsed since midnight.

範例

下列範例會顯示 DateTime 值之陣列的 TimeOfDay 屬性值。The following example displays the value of the TimeOfDay property for an array of DateTime values. 它也會在複合格式作業中,將傳回值與 "t" 標準格式字串所傳回的字串進行對比。It also contrasts the return value with the string returned by the "t" standard format string in a composite formatting operation.

using System;

public class Example
{
   public static void Main()
   {
      DateTime[] dates = { DateTime.Now, 
                           new DateTime(2013, 9, 14, 9, 28, 0),
                           new DateTime(2011, 5, 28, 10, 35, 0),
                           new DateTime(1979, 12, 25, 14, 30, 0) };
      foreach (var date in dates) {
         Console.WriteLine("Day: {0:d} Time: {1:g}", date.Date, date.TimeOfDay);
         Console.WriteLine("Day: {0:d} Time: {0:t}\n", date);
      }                              
   }
}
// The example displays output like the following:
//    Day: 7/25/2012 Time: 10:08:12.9713744
//    Day: 7/25/2012 Time: 10:08 AM
//    
//    Day: 9/14/2013 Time: 9:28:00
//    Day: 9/14/2013 Time: 9:28 AM
//    
//    Day: 5/28/2011 Time: 10:35:00
//    Day: 5/28/2011 Time: 10:35 AM
//    
//    Day: 12/25/1979 Time: 14:30:00
//    Day: 12/25/1979 Time: 2:30 PM
Module Example
   Public Sub Main()
      Dim dates() As Date = { Date.Now, 
                              New DateTime(2013, 9, 14, 9, 28, 0),
                              New DateTime(2011, 5, 28, 10, 35, 0),
                              New DateTime(1979, 12, 25, 14, 30, 0) }
      For Each dat In dates
         Console.WriteLine("Day: {0:d} Time: {1:g}", dat.Date, dat.TimeOfDay)
         Console.WriteLine("Day: {0:d} Time: {0:t}", dat)
         Console.WriteLine()
      Next                              
   End Sub
End Module
' The example displays output like the following:
'    Day: 7/25/2012 Time: 10:08:12.9713744
'    Day: 7/25/2012 Time: 10:08 AM
'    
'    Day: 9/14/2013 Time: 9:28:00
'    Day: 9/14/2013 Time: 9:28 AM
'    
'    Day: 5/28/2011 Time: 10:35:00
'    Day: 5/28/2011 Time: 10:35 AM
'    
'    Day: 12/25/1979 Time: 14:30:00
'    Day: 12/25/1979 Time: 2:30 PM

備註

不同于 Date 屬性。Unlike the Date property. 這會傳回代表日期而不含時間元件的 @no__t 0 值,@no__t 1 屬性會傳回代表 @no__t 3 值之時間元件的 TimeSpan 值。which returns a DateTime value that represents a date without its time component, the TimeOfDay property returns a TimeSpan value that represents a DateTime value's time component.

如果您想要顯示一天中的時間,或取出 DateTime 值之日期的字串表示,您可以改為呼叫具有 @no__t 2 參數之 ToString 方法的多載,或使用複合格式功能搭配 "t" 或 "t" standard 格式字串。If you want to display the time of day or retrieve the string representation of the time of day of a DateTime value, you can instead call an overload of the ToString method that has a format parameter or use the composite formatting feature with the "t" or "T" standard format string.

適用於

另請參閱