我有2日期時間的:添加時間跨度只有日期,而不是日期的時間
DateTime beginDate = 2000/01/01 14:00
DateTime endDate = 2000/01/01 14:30
我計算這些2小時之間的時間跨度:
TimeSpan span = endDate.Subtract(beginDate);
var myValue = beginDate.AddMinutes(span.Minutes).TimeOfDay.Ticks
//Trying to get this equal to the endDate by using the beginDate + myValue
//I have to use myvalue, because this value comes from the DB and this piece
//of code sits in another class than the above code
DateTime otherDate = beginDate.Date.Add(new TimeSpan(myValue))
問題是我不斷獲取0: 30回來,我應該回到14:30。 我明白爲什麼,因爲beginDate.Date給你2000/01/01 00:00,但我不能使用beginDate.TimeOfDay.Add,因爲它是隻讀字段。 我如何實現它只將myValue添加到給定日期的時間?
這是有點不清楚你想要做什麼,爲什麼你跳過這麼多籃球。 – Rotem
通過使用beginDate和myValue來計算並獲得與endDate相同的結果 – user1702369
不清楚'myValue'是什麼意思。如果'beginDate' +'myValue'應該等於'endDate',那麼'myValue'應該等於'span'。爲什麼所有的'.TimeOfDay','.Date','。Minutes','.Ticks'混淆?如果您只是試圖序列化TimeSpan來保存數據庫,請使用'.Ticks'屬性。 – Rotem