共用方式為


DateTimeOffset.Subtraction 運算子

定義

DateTimeOffset 物件減去指定的 TimeSpanDateTimeOffset 物件。

多載

Subtraction(DateTimeOffset, DateTimeOffset)

從其他 DateTimeOffset 物件減去一個物件並產生時間間隔。

Subtraction(DateTimeOffset, TimeSpan)

從指定的日期和時間減去指定的時間間隔,並產生新的日期和時間。

Subtraction(DateTimeOffset, DateTimeOffset)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

從其他 DateTimeOffset 物件減去一個物件並產生時間間隔。

public:
 static TimeSpan operator -(DateTimeOffset left, DateTimeOffset right);
public static TimeSpan operator - (DateTimeOffset left, DateTimeOffset right);
static member ( - ) : DateTimeOffset * DateTimeOffset -> TimeSpan
Public Shared Operator - (left As DateTimeOffset, right As DateTimeOffset) As TimeSpan

參數

left
DateTimeOffset

被減數。

right
DateTimeOffset

減數。

傳回

物件,代表 leftright 之間的差。

備註

方法 Subtraction 會定義 物件的 DateTimeOffset 減法運算。 它會啟用下列程式碼:

DateTimeOffset firstDate = new DateTimeOffset(2008, 3, 25, 18, 0, 0,
                                              new TimeSpan(-7, 0, 0));
DateTimeOffset secondDate = new DateTimeOffset(2008, 3, 25, 18, 0, 0,
                                               new TimeSpan(-5, 0, 0));
DateTimeOffset thirdDate = new DateTimeOffset(2008, 2, 28, 9, 0, 0,
                                              new TimeSpan(-7, 0, 0));
TimeSpan difference;

difference = firstDate - secondDate;
Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}",
                  firstDate.ToString(),
                  secondDate.ToString(),
                  difference.Days,
                  difference.Hours,
                  difference.Minutes);

difference = firstDate - thirdDate;
Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}",
                  firstDate.ToString(),
                  thirdDate.ToString(),
                  difference.Days,
                  difference.Hours,
                  difference.Minutes);
// The example produces the following output:
//    (3/25/2008 6:00:00 PM -07:00) - (3/25/2008 6:00:00 PM -05:00): 0 days, 2:00
//    (3/25/2008 6:00:00 PM -07:00) - (2/28/2008 9:00:00 AM -07:00): 26 days, 9:00
let firstDate =
    DateTimeOffset(2008, 3, 25, 18, 0, 0, TimeSpan(-7, 0, 0))

let secondDate =
    DateTimeOffset(2008, 3, 25, 18, 0, 0, TimeSpan(-5, 0, 0))

let thirdDate =
    DateTimeOffset(2008, 2, 28, 9, 0, 0, TimeSpan(-7, 0, 0))

let difference = firstDate - secondDate
printfn $"({firstDate}) - ({secondDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}"

let difference = firstDate - thirdDate
printfn $"({firstDate}) - ({secondDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}"
// The example produces the following output:
//    (3/25/2008 6:00:00 PM -07:00) - (3/25/2008 6:00:00 PM -05:00): 0 days, 2:00
//    (3/25/2008 6:00:00 PM -07:00) - (3/25/2008 6:00:00 PM -05:00): 26 days, 9:00
Dim firstDate As New DateTimeOffset(#3/25/2008 6:00PM#, _
                                    New TimeSpan(-7, 0, 0))
Dim secondDate As New DateTimeOffset(#3/25/2008 6:00PM#, _
                                     New TimeSpan(-5, 0, 0))
Dim thirdDate As New DateTimeOffset(#2/28/2008 9:00AM#, _
                                    New TimeSpan(-7, 0, 0))
Dim difference As TimeSpan

difference = firstDate - secondDate
Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}", _
                  firstDate.ToString(), _
                  secondDate.ToString(), _
                  difference.Days, _
                  difference.Hours, _
                  difference.Minutes)      
difference = firstDate - thirdDate
Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}", _
                  firstDate.ToString(), _
                  secondDate.ToString(), _
                  difference.Days, _
                  difference.Hours, _
                  difference.Minutes) 
' The example produces the following output:
'    (3/25/2008 6:00:00 PM -07:00) - (3/25/2008 6:00:00 PM -05:00): 0 days, 2:00
'    (3/25/2008 6:00:00 PM -07:00) - (3/25/2008 6:00:00 PM -05:00): 26 days, 9:00

不支援自訂運算子和運算子多載的語言可以改為呼叫 DateTimeOffset.Subtract(DateTimeOffset) 方法。

這個運算子 DateTimeOffset.Subtract(TimeSpan) 的對等方法是 。

另請參閱

適用於

Subtraction(DateTimeOffset, TimeSpan)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

從指定的日期和時間減去指定的時間間隔,並產生新的日期和時間。

public:
 static DateTimeOffset operator -(DateTimeOffset dateTimeOffset, TimeSpan timeSpan);
public:
 static DateTimeOffset operator -(DateTimeOffset dateTimeTz, TimeSpan timeSpan);
public static DateTimeOffset operator - (DateTimeOffset dateTimeOffset, TimeSpan timeSpan);
public static DateTimeOffset operator - (DateTimeOffset dateTimeTz, TimeSpan timeSpan);
static member ( - ) : DateTimeOffset * TimeSpan -> DateTimeOffset
static member ( - ) : DateTimeOffset * TimeSpan -> DateTimeOffset
Public Shared Operator - (dateTimeOffset As DateTimeOffset, timeSpan As TimeSpan) As DateTimeOffset
Public Shared Operator - (dateTimeTz As DateTimeOffset, timeSpan As TimeSpan) As DateTimeOffset

參數

dateTimeOffsetdateTimeTz
DateTimeOffset

當做被減數的日期和時間物件。

timeSpan
TimeSpan

要減去的時間間隔。

傳回

物件,等於 dateTimeOffset 減去 timeSpan 的值。

例外狀況

備註

方法 Subtraction 會定義 物件的 DateTimeOffset 減法運算。 它會啟用下列程式碼:

DateTimeOffset offsetDate = new DateTimeOffset(2007, 12, 3, 11, 30, 0,
                               new TimeSpan(-8, 0, 0));
TimeSpan duration = new TimeSpan(7, 18, 0, 0);
Console.WriteLine(offsetDate - duration);  // Displays 11/25/2007 5:30:00 PM -08:00
let offsetDate =
    DateTimeOffset(2007, 12, 3, 11, 30, 0, TimeSpan(-8, 0, 0))

let duration = TimeSpan(7, 18, 0, 0)
printfn $"{offsetDate - duration}" // Displays 11/25/2007 5:30:00 PM -08:00
Dim offsetDate As New DateTimeOffset(#12/3/2007 11:30AM#, _
                               New TimeSpan(-8, 0, 0)) 
Dim duration As New TimeSpan(7, 18, 0, 0)
Console.WriteLine(offsetDate - duration)    ' Displays 11/25/2007 5:30:00 PM -08:00

不支援自訂運算子和運算子多載的語言可以改為呼叫 DateTimeOffset.Subtract(TimeSpan) 方法。

這個運算子 DateTimeOffset.Subtract(TimeSpan) 的對等方法是 。

另請參閱

適用於