C# 字符串未被识别为有效的日期时间。将字符串转换为日期时间
我需要在下面的代码中将C# 字符串未被识别为有效的日期时间。将字符串转换为日期时间,c#,C#,我需要在下面的代码中将字符串转换为日期时间格式。但我遇到了以下错误。我怎样才能解决这个问题 未处理的异常: System.FormatException:未将字符串识别为有效的日期时间 publicstaticintcalculateage(字符串dateOfBirth) { //在这里实现代码 int年龄=0; DateTime s=DateTime.ParseExact(dateOfBirth,“D”,null); 年龄=转换为32(DateTime.Now.Year-s.Year); if
字符串
转换为日期时间
格式。但我遇到了以下错误。我怎样才能解决这个问题
未处理的异常:
System.FormatException:未将字符串识别为有效的日期时间
publicstaticintcalculateage(字符串dateOfBirth)
{
//在这里实现代码
int年龄=0;
DateTime s=DateTime.ParseExact(dateOfBirth,“D”,null);
年龄=转换为32(DateTime.Now.Year-s.Year);
if(DateTime.Now.DayOfYear
这是学习阅读文档的好时机。我们正在处理DateTime.ParseExact()
方法。此方法有几个重载,但我们关心的重载的文档如下:
我通过在谷歌搜索C#DateTime.ParseExact()
找到了这个链接。我找到了第一个搜索结果,然后单击页面上的第一个链接以找到正确的重载
我们看到该方法的第二个参数(您提供的“D”
)是一个格式字符串。该论点的注释参考备注部分,我们在其中发现:
format
参数是一个字符串,它包含一个标准格式说明符,或一个或多个自定义格式说明符,用于定义所需的格式。有关有效格式代码的详细信息,请参阅或
按照标准格式字符串的链接,我们最终可以找到关于“D”格式的以下信息:
“D”长日期模式。2009年6月15日(星期一)
如果没有页面上的完整上下文,这有点令人困惑,但这告诉您,您的dateOfBirth
字符串将精确匹配2009年6月15日星期一所描述的模式(dddddd,MMMM dd,yyyy
)。基于,字符串看起来更像15-06-2009
(dd-mm-yyyy
)。这些不匹配,这就是您看到错误的原因
要解决此问题,您需要找到一种格式,而不是“D”
,以便与ParseExact()
方法一起使用,该方法将与输入字符串使用的格式完全匹配
同样,这看起来像是一个学习环境,所以我将把它留给您来解决。正确的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateEx1 //DO NOT CHANGE the namespace name
{
public class Program //DO NOT CHANGE the class name
{
public static void Main(string[] args) //DO NOT CHANGE the 'Main' method signature
{
Console.WriteLine("Enter the date of birth (dd-mm-yyyy) ");
string dateOfBirth=Console.ReadLine();
Console.WriteLine(calculateAge(dateOfBirth));
}
public static int calculateAge(string dateOfBirth)
{
//Implement code here
//int age;
DateTime s = DateTime.ParseExact(dateOfBirth, "dd-mm-yyyy",null);
DateTime today = DateTime.Now;
int year = DateTime.Now.Year;
int length=dateOfBirth.Length;
string dcs = dateOfBirth.Substring(6,4);
string csd=dateOfBirth.Substring(3,2);
int age2=int.Parse(csd);
if(age2>=6)
{
int age=int.Parse(dcs);
int age1=(year-1)-age;
return age1;
}
else
{
int age=int.Parse(dcs);
int age3=(year)-age;
return age3;
}
}
}
}
也许可以尝试DateTime.Parse
,以允许更多格式工作。否则,您需要确保字符串与格式匹配。“D”是长日期格式,因此它必须类似于“2009年6月15日,星期一”字符串dateOfBirth中的值是多少?也不要垃圾发送C#version标签。只有当问题特定于某个给定的版本,并且很可能只特定于某个版本时,才需要它们。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateEx1 //DO NOT CHANGE the namespace name
{
public class Program //DO NOT CHANGE the class name
{
public static void Main(string[] args) //DO NOT CHANGE the 'Main' method signature
{
Console.WriteLine("Enter the date of birth (dd-mm-yyyy) ");
string dateOfBirth=Console.ReadLine();
Console.WriteLine(calculateAge(dateOfBirth));
}
public static int calculateAge(string dateOfBirth)
{
//Implement code here
//int age;
DateTime s = DateTime.ParseExact(dateOfBirth, "dd-mm-yyyy",null);
DateTime today = DateTime.Now;
int year = DateTime.Now.Year;
int length=dateOfBirth.Length;
string dcs = dateOfBirth.Substring(6,4);
string csd=dateOfBirth.Substring(3,2);
int age2=int.Parse(csd);
if(age2>=6)
{
int age=int.Parse(dcs);
int age1=(year-1)-age;
return age1;
}
else
{
int age=int.Parse(dcs);
int age3=(year)-age;
return age3;
}
}
}
}