I m failing to convert an ISO DateTime format value to date value only. I tried Parse, ParseExact and TryParseExact and all failed and keep giving me the same value output (1984-04-26T00:00:00).
I have referred to many references and other SO questions but none helped
I am using a DTO and this is the property,
public DateTime Dob { get; set; }
I am running a for loop to join data from my LINQ queries and I am trying to do my conversion as follows,
for (int i = 0; i < PatientInfo.Count; i++)
{
PatientInfo[i].Dob =
DateTime.Parse(String.Format("{0:MM/dd/yyyy}", PatientInfo[i].Dob.ToString()));
PatientInfo[i].PartnerData = PartnerInfo.Where(a => a.FileId == PartnerInfo[i].FileId).ToList();
}
What am I missing here?
the following is the API controller code,
[HttpGet("{id}")]
public async Task<IActionResult> GetPatReg([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var PatientInfo = await _context.PatReg
.Where(a => a.FileId == id)
.Select(b => new PatRegDto
{
Action = "Get",
FileId = b.FileId,
FName = b.FName,
MName = b.MName,
LName = b.LName,
Dob = b.Dob
}).ToListAsync();
var PartnerInfo = await _context.PatPar
.Where(s => s.FileId == id)
.Select(m => new PatParDto
{
RecId = m.RecId,
FileId = m.FileId,
ParFileId = m.ParFileId,
SDate = m.SDate,
EDate = m.EDate,
}).ToListAsync();
for (int i = 0; i < PartnerInfo.Count; i++)
{
PartnerInfo[i].FullName = _context.PatReg.Where(a => a.FileId == PartnerInfo[i].ParFileId)
.Select(t => new { t.fullname })
.Single().fullname;
PartnerInfo[i].dob = _context.PatReg.Where(a => a.FileId == PartnerInfo[i].ParFileId)
.Select(t => new { t.Dob })
.Single().Dob;
PartnerInfo[i].Action = "Get";
}
for (int i = 0; i < PatientInfo.Count; i++)
{
PatientInfo[i].Dob =
DateTime.Parse(String.Format("{0:MM/dd/yyyy}", PatientInfo[i].Dob.ToString()));
PatientInfo[i].PartnerData = PartnerInfo.Where(a => a.FileId == PartnerInfo[i].FileId).ToList();
}
if (PatientInfo == null)
{
return NotFound();
}
var DataRes = new
{
sdata = PatientInfo
};
return Ok(DataRes);
}
Update My DTOs are
public class PatRegDto
{
public string Action { get; set; }
private Int64 _FileId;
public Int64 FileId
{
get
{
return this._FileId;
}
set
{
this._FileId = value;
}
}
public string FName { get; set; }
public string MName { get; set; }
public string LName { get; set; }
public string fullname
{
get { return FName + " " + MName + " " + LName; }
}
public DateTime Dob { get; set; }
public List<PatParDto> PartnerData { get; set; }
}
public class PatParDto
{
public string Action { get; set; }
public long RecId { get; set; }
public long FileId { get; set; }
public long ParFileId { get; set; }
public DateTime SDate { get; set; }
public DateTime? EDate { get; set; }
public DateTime dob { get; set; }
public string FullName { get; set; }
}
Please do not use
PatientInfo[i].Dob.ToString())because it will be serialized to string using your current culture settings. Depends on your OS language settings you will have different string. Beside thatDateTimecontains Date property that returns date ;)