我正在尝试使用基于日期/时间对象值的if else语句显示特定字符串。
我正在使用一个公式编辑器来检查语法,但我没有遇到任何问题,但是当在应用程序中解析公式时,我会得到满足条件的空白。
这是我的公式:
if (System.Convert.ToInt32(rpt.Fields["AppointmentResourceLink.Appointment.Patient.DateDied"].Value)>0)
{
return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value) + '\n' + "Deceased: " + System.String.Format("{0:dd/MM/yyyy}",rpt.Fields["AppointmentResourceLink.Appointment.Patient.DateDied"].Value);
}
else
{
return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value);
}
公式编辑器允许转换为不同的数据类型,我尝试修改公式,返回值为布尔值,但这也不起作用。
由于
答案 0 :(得分:4)
Convert.ToInt32方法不支持从DateTime到int的转换。
答案 1 :(得分:0)
这是有用的:
// ListFields();
试 { if(System.Convert.ToInt32(System.String.Format(" {0:yyyy}",rpt.Fields [" AppointmentResourceLink.Appointment.Patient.DateDied"]。Value) )→1) 返回"已故日期:" + System.String.Format(" {0:dd / MM / yyyy}",rpt.Fields [" AppointmentResourceLink.Appointment.Patient.DateDied"]。Value); }
catch(System.Exception e)
{
System.String.Format(" {0:DD / MM / YYYY}",rpt.Fields [" AppointmentResourceLink.Appointment.Patient.DateDied"]值);
}
返回"";
答案 2 :(得分:-1)
请尝试使用此代码,这将符合您的目的,并根据需要为您提供输出。
try
{
string s = rpt.Fields["AppointmentResourceLink.Appointment.Patient.DateDied"].Value;
// Please replace your datetime format in ParseExact
DateTime dt = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
}
catch
{
return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value);
}
if (dt != DateTime.MinValue)
{
return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value) + '\n' + System.String.Format("font-style:Bold", "Deceased: ") + System.String.Format("{0:dd/MM/yyyy}",rpt.Fields["AppointmentResourceLink.Appointment.Patient.DateDied"].Value);
}
else
{
return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value);
}