如何在 C# 中将 iso 日期时间转换为日期?
how to convert iso datetime to date in c#?
我无法将 ISO
DateTime
格式值仅转换为日期值。我尝试了 Parse
、ParseExact
和 TryParseExact
,但都失败了,并继续给我相同的值输出 (1984-04-26T00:00:00
)。
我提到了很多 references 和其他 SO 问题,但 none 帮助了
我正在使用 DTO,这是 属性,
public DateTime Dob { get; set; }
我是 运行 一个 for
循环来连接来自我的 LINQ
查询的数据,我正在尝试按如下方式进行转换,
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();
}
我在这里错过了什么?
下面是API controller
代码,
[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);
}
更新
我的 DTO 是
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; }
}
请不要使用 PatientInfo[i].Dob.ToString())
,因为它将使用您当前的区域性设置序列化为字符串。根据您的 OS 语言设置,您会有不同的字符串。除此之外 DateTime
包含 Date 属性 returns 日期 ;)
那么这是唯一的解决方案吗?
我将 public string DobFormat { get; set; }
添加到 PatRegDto
DTO
然后,
for (int i = 0; i < PatientInfo.Count; i++)
{
PatientInfo[i].DobFormat = PatientInfo[i].Dob.ToString("MM/dd/yyyy");
PatientInfo[i].PartnerData = PartnerInfo.Where(a => a.FileId == PartnerInfo[i].FileId).ToList();
}
以上所有宝贵意见的另一个答案,
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 string Dob { get; set; }
public List<PatParDto> PartnerData { get; set; }
}
在控制器中
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).ToString("dd/M/yyyy"),
}).ToListAsync();
我无法将 ISO
DateTime
格式值仅转换为日期值。我尝试了 Parse
、ParseExact
和 TryParseExact
,但都失败了,并继续给我相同的值输出 (1984-04-26T00:00:00
)。
我提到了很多 references 和其他 SO 问题,但 none 帮助了
我正在使用 DTO,这是 属性,
public DateTime Dob { get; set; }
我是 运行 一个 for
循环来连接来自我的 LINQ
查询的数据,我正在尝试按如下方式进行转换,
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();
}
我在这里错过了什么?
下面是API controller
代码,
[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);
}
更新 我的 DTO 是
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; }
}
请不要使用 PatientInfo[i].Dob.ToString())
,因为它将使用您当前的区域性设置序列化为字符串。根据您的 OS 语言设置,您会有不同的字符串。除此之外 DateTime
包含 Date 属性 returns 日期 ;)
那么这是唯一的解决方案吗?
我将 public string DobFormat { get; set; }
添加到 PatRegDto
DTO
然后,
for (int i = 0; i < PatientInfo.Count; i++)
{
PatientInfo[i].DobFormat = PatientInfo[i].Dob.ToString("MM/dd/yyyy");
PatientInfo[i].PartnerData = PartnerInfo.Where(a => a.FileId == PartnerInfo[i].FileId).ToList();
}
以上所有宝贵意见的另一个答案,
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 string Dob { get; set; }
public List<PatParDto> PartnerData { get; set; }
}
在控制器中
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).ToString("dd/M/yyyy"),
}).ToListAsync();