2

I saw lots of duplicate post of this But for me it's something different.

I have a Datetime object and get time portion & assign another time to that.When i'm going to assign it it raise those error.

in here newStartDateGroup is a DateTime Object in here OpenTime is a TimeSpan

Property or indexer cannot be assigned to — it is read only

else if(newStartDateGroup.TimeOfDay < i.OpenTime && newEndDateGroup.TimeOfDay > i.CloseTime) // < >
 {
        newStartDateGroup.TimeOfDay = i.OpenTime;
4

3 回答 3

7

DateTime.NET 中的structimmutable,因此您不必更改它的值,而是必须创建新的值:

newStartDateGroup = newStartDateGroup.Date.Add(i.OpenTime);
于 2013-08-20T05:06:34.710 回答
3

您只能创建新DateTime实例。

根据您显示的代码,您只想设置白天时间。
这应该这样做:

newStartDateGroup = DateTime.Today.Add(i.OpenTime);

DateTime.Today今天是 00:00。

于 2013-08-20T05:05:15.630 回答
1

只需手动分配小时、分钟和秒。

DateTime temp = DateTime(newStartDateGroup.Year, newStartDateGroup.Month, newStartDateGroup.Day, i.OpenTime.Hours, i.OpenTime.Minutes, i.OpenTime.Seconds);

newStartDateGroup = temp;
于 2013-08-20T05:07:50.220 回答