如何计算现在的时间与“scheduledstart”字段(在约会实体中)之间的差异,并将值放入另一个数字字段(“el_early_cancel_notice” - 类似于持续时间字段)。 这是我写的,直到现在,但我不知道如何继续。有人可以帮忙吗?
if ((serviceAppontment.Attributes.Contains("scheduledstart")) && (serviceAppontment.Attributes["scheduledstart"] != null) && (TimeNow > SchelStart))
{
throw new InvalidPluginExecutionException(serviceAppontment.Attributes["scheduledstart"].ToString());
try
{
//here i need to calculate the difference and i don't know where to begin...
}
答案 0 :(得分:0)
DateTime startDate = (DateTime)serviceAppontment["scheduledstart"];
TimeSpan diff = DateTime.UtcNow - startDate;
serviceAppointment["el_early_cancel_notice"] = diff.Days;
localContext.OrganizationService.Update(serviceAppoinment);
由于DateTime以UTC格式存储在CRM中,因此您需要以UTC格式处理DateTime。我假设你想要持续时间。有关TimeSpan的更多信息here。
祝你好运!答案 1 :(得分:-1)
1)将您的字段投射到日期时间:
DateTime startDate =(DateTime)serviceAppontment.Attributes [“scheduledstart”];
2)使用these methods之一获取TimeSpan。
3)使用TimeSpan中的信息更新el_early_cancel_notice字段: serviceAppontment.Attributes [“el_early_cancel_notice”] = [来自TimeSpan的适当号码];