下面这段代码给我下面的错误:难点解析字符串为DateTime
"Unhandled Exception: string was not recognized as a valid DateTime.
有一个未知的词开始在索引0”
我如何将字符串转换为DateTime正确?这里
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter your birthday in format(dd.mm.yyyy): ");
DateTime userBirthday = DateTime.Parse(Console.ReadLine());
long result = DateTime.Today.Subtract(userBirthday).Ticks;
Console.WriteLine("You are {0} years old.", new DateTime(result).Year - 1);
Console.WriteLine("After 10 years you will be {0} years old.", new DateTime(result).AddYears(10).Year - 1);
}
}
}
使用'DateTime.TryParse' /'DateTime.TryParseExact'如果你得到用户输入。除此之外,用户给你什么作为输入?您当前的日期设置是什么(控制面板/区域和语言)。 –