I have a DTO field in DateTime format
public DateTime date_joined { get; set; }
I use this to turn the data into a Json
public JsonResult Customer(int? id)
{
var user = User.Identity.Name;
if (!AccountController.IsInRole(user, "admin"))
{
return null;
}
id = id == null ? 0 : id;
var customer = db.PrmCustomers
.Where(x => x.EmailAddress != null && x.CustomerID > id)
.OrderBy(x => x.CustomerID)
.Select(x => new CustomerDTO()
{
email = x.EmailAddress,
phone = x.MobilePhoneNumber,
username = "",
first_name = x.Name,
last_name = x.Surname,
gender = x.GenderType,
customer_code = x.CustomerID,
is_active = "",
//date_joined = String.Format("{0:d/M/yyyy HH:mm:ss}", x.CreateDate.ToString()),
date_joined = x.CreateDate,
last_login = "",
date_of_birth = x.BirthDate,
//date_of_birth = String.Format("{0:d/M/yyyy HH:mm:ss}", x.BirthDate.ToString()),
password = x.Password,
email_allowed = x.IsNewsletter,
sms_allowed = x.IsSms,
verified = x.IsApprovedEmail,
social_account_facebook_uuid = "",
social_account_facebook_extra = ""
});
return Json(customer, JsonRequestBehavior.AllowGet);
}
Problem is, it comes up as this
"date_joined":"\/Date(1516965473683)\/"
I tried to change it into another format so far I couldn't manage it.
First I tried the usual DateTime formatting as toString("newFOrmat")
but I got Linq errors mostly because toString() is not recognized in SQL Server
Then I came across this question format date in linq query result and tried the method there as this
return Json(customer.AsEnumerable().Select(r=>new CustomerDTO{
date_joined = r.date_joined.GetValueOrDefault().ToString("dd.MM.yyyy")
}) , JsonRequestBehavior.AllowGet);
I got "DateTime does not have a definition for GetValueOf()" error although I have the correct namespaces included.
Omitting it and using only the usual ToString("format") brought the Linq error above.
Any other suggestions?
EDIT: My DTO and Output has other fields too. I didn't include them in the question.