我有一个视图模型,我在其中存储DateTime但在我的视图中使用a JQUERY datetimepicker,仅限时间:
视图模型
[DataType(DataType.Time)]
public DateTime? MondayFrom { get; set; }
[DataType(DataType.Time)]
public DateTime? MondayTo { get; set; }
Run Code Online (Sandbox Code Playgroud)
就目前而言,当调用Create方法时,它使用今天的日期加上从中选择的时间timepicker.
由于我并不特别关注日期部分DateTime,我想将日期,月份和年份更改为01/01/1900或者比记录编写日期更不具体的内容,在创建记录之前,这纯粹是以避免将来出现任何混淆.
我不想陷入这样做是否正确的事情.
我很难掌握DateTime的Date部分,见下文:
public void CreateClub(Club club)
{
foreach (var item in club.MeetingDays)
{
// change Date part to 01/01/1900, leave the time as is..
}
_repository.CreateClub(club);
}
Run Code Online (Sandbox Code Playgroud)
我怎么可能把日期部分放在item一边但是把时间留在一边?
只需使用该TimeOfDay属性提取当天的时间,并将其添加到您想要的日期:
private static readonly DateTime BaseDate = new DateTime(1900, 1, 1);
var updatedDateTime = BaseDate + otherDateTime.TimeOfDay;
Run Code Online (Sandbox Code Playgroud)
你甚至可以写一两个扩展方法:
public static DateTime WithDate(this DateTime start, DateTime date)
{
// No need to use the Date property if you already know
// it will be midnight...
return date.Date + start.TimeOfDay;
}
public static DateTime WithFloorDate(this DateTime start)
{
return start.WithDate(FloorDate);
}
Run Code Online (Sandbox Code Playgroud)
当然,我建议你使用Noda Time来指定日期,时间和日期/时间值(分别有或没有时区或UTC offset0,但这是一个不同的对话.
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |