【发布时间】:2017-06-17 23:08:01
【问题描述】:
大家好。我是 C# 新手,但我似乎无法理解 DateTime 是如何工作的。 我想做的就是检查如果 (givenday) = 今天并且时间是晚上 7 点,我想返回 true。这是正确的做法吗?
注意ActionDate是用户输入的字段。
DateTime dateA = Convert.ToDateTime(ActionDate);
int a = dateA.Year;
int b = dateA.Month;
int c = dateA.Day;
int d = timeA.Hour;
int e = timeA.Minute;
var newDate = new DateTime(a, b, c, d, e, 0);
DateTime end = Convert.ToDateTime(newDate);
DateTime start = Convert.ToDateTime(A);
TimeSpan span = end.Subtract(start);
Decimal minutes = Convert.ToDecimal(span.TotalMinutes);
if
{
return true;
} else
{
return false;
}
提前谢谢你。
【问题讨论】:
-
为什么要将 DateTime 转换为 DateTime?
-
假设
ActionDate已经是DateTime,这就是你所需要的:return ActionDate.Date == DateTime.Today && ActionDate.Hour == 19;。如果你想包括分钟添加&& ActionDate.Minutes == 0 -
我完全不确定先生。只是看到这个 stackoverflow 答案,所以我也尝试一下。
-
它工作哈哈感谢蒂姆接受了你的评论和尼诺。谢谢大家
标签: c#