gpt4 book ai didi

c# - Convert.ToDateTime 出错

转载 作者:行者123 更新时间:2023-11-30 15:16:10 24 4
gpt4 key购买 nike

我有这个代码:

public JsonResult salvaPaciente(string cns, string sexo, string dataNasc, string nome, string raca,
string cep, string ibge, string logradouro, string endereco, string numero,
string complemento, string bairro, string telefone, string email)
{
byte[] bytes = System.Text.Encoding.GetEncoding("iso-8859-8").GetBytes(nome);
nome = System.Text.Encoding.UTF8.GetString(bytes);

paciente oPaciente = new paciente()
{
bairro = bairro,
cep = cep,
cns = cns,
complemento = complemento,
dataNasc = Convert.ToDateTime(dataNasc),
email = email == "" ? " " : email,
endereco = endereco,
ibge = ibge,
logradouro = logradouro,
nacionalidade = "010",
nomePaciente = nome,
numero = numero,
raca = raca,
sexo = sexo,
telefone = telefone
};
modelOff.pacientes.Add(oPaciente);
try
{
modelOff.SaveChanges();
return Json("sim", JsonRequestBehavior.AllowGet);
}
catch
{
return Json("nao", JsonRequestBehavior.AllowGet);
}
}

在我实际使用 Visual Studio 2017 并生成以下错误后,代码正常运行并崩溃:

[FormatException: String was not recognized as a valid DateTime.]

字符串 dataNasc 包含 '24/06/1979'

最佳答案

You can use DateTime.ParseExact

样本:-

string dataNasc = "24/06/1979";
DateTime time = DateTime.ParseExact(dataNasc, "dd/MM/yyyy", null);

Online Example

关于c# - Convert.ToDateTime 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50411698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com