Reputation: 3707
I'm trying to get some data to format in the ShortDateTime format of "MM/dd/yyyy" but everything I'm trying is not working.
I have the following EF below and the field I'm specifically looking at is the ExpireDate. When I try to use a DateTime.Parse around the field I get an error cannot convert from 'System.DateTime?" to 'string'
. Without the DateTime.Parse then the date displays as 3/15/2017 12:00:00 AM
.
I tried using Data Annotations on the Model as well but that doesn't seem to work either.
Any suggestions?
EF query
data = (from e in ctx.clock_emp_excep_hist
join g in ctx.clock_group on e.group_id equals g.group_id
join emp in ctx.employee on e.emp_id equals emp.emp_id
where e.group_id == 4
select new
{
UpdateDate = e.create_date,
UpdateType = e.add_delete_ind,
GroupId = e.group_id,
GroupName = g.group_name,
Reason = e.reason,
ExpireDate = e.expiration_date,
UpdateEmployeeId = e.emp_id,
UpdateFirstName = emp.emp_firstname,
UpdateLastName = emp.emp_lastname
}).Select(e => new EmployeeExceptionLog {
UpdateDate = e.UpdateDate,
UpdateType = e.UpdateType == "A" ? "Add" : "Delete",
GroupId = e.GroupId,
GroupName = e.GroupName,
Reason = e.Reason,
ExpireDate = DateTime.Parse(e.ExpireDate),
UpdateEmployeeId = e.UpdateEmployeeId,
UpdateFirstName = e.UpdateFirstName,
UpdateLastName = e.UpdateLastName
}).ToList();
}
Property in class
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode=true)]
public DateTime? ExpireDate { get; set; }
More info:
This is how I'm displaying the data in the View.
@foreach (var item in Model.AuditLogs)
{
<tr class="@(item.UpdateType == "Delete" ? "danger" : "")">
<td>@item.UpdateDate</td>
<td>@item.UpdateType</td>
<td>@item.Reason</td>
<td>@item.ExpireDate</td>
<td>@item.UpdateEmployeeId - @item.UpdateFirstName @item.UpdateLastName</td>
</tr>
}
The DataType in the Oracle Database is DateTime?.
Upvotes: 2
Views: 1809
Reputation: 3625
You can't parse nullable DateTime. To convert nullable DateTime into a formatted string :
ExpireDate != null ? ExpireDate.Value.ToString("MM/dd/yyyy") : "ERROR";
Update your query into this:
data = (from e in ctx.clock_emp_excep_hist
join g in ctx.clock_group on e.group_id equals g.group_id
join emp in ctx.employee on e.emp_id equals emp.emp_id
where e.group_id == 4
select new
{
UpdateDate = e.create_date,
UpdateType = e.add_delete_ind,
GroupId = e.group_id,
GroupName = g.group_name,
Reason = e.reason,
ExpireDate = e.expiration_date,
UpdateEmployeeId = e.emp_id,
UpdateFirstName = emp.emp_firstname,
UpdateLastName = emp.emp_lastname
}).Select(e => new EmployeeExceptionLog {
UpdateDate = e.UpdateDate,
UpdateType = e.UpdateType == "A" ? "Add" : "Delete",
GroupId = e.GroupId,
GroupName = e.GroupName,
Reason = e.Reason,
ExpireDate = ExpireDate != null ? ExpireDate.Value.ToString("MM/dd/yyyy") : "ERROR",
UpdateEmployeeId = e.UpdateEmployeeId,
UpdateFirstName = e.UpdateFirstName,
UpdateLastName = e.UpdateLastName
}).ToList();
}
Upvotes: 0
Reputation: 2125
public DateTime? ExpireDate { get; set; }
Its nullable date so you can have Null value for ExpireDate.
"cannot convert from 'System.DateTime?" to 'string'"
ExpireDate = DateTime.Parse(e.ExpireDate),
DateTime.Parse takes string as a parameter.
i would change this to
ExpireDate = ((e.ExpireDate != null) ? (DateTime)e.ExpireDate : "SOME DEFAULT DATE"),
Also in View you can display using ToShortDateString() if your model property is dateTime.
<td>@item.ExpireDate.ToShortDateString()</td>
here is more info about DateTime.
Upvotes: 1