很简单
using System;
using System.Timers;
using System.Threading;
class Test
{
static void Main()
{
string mystring = "2007年1月1日";
try
{
DateTime mydate = Convert.ToDateTime(mystring);
Console.WriteLine(mydate);
}
catch
{
Console.WriteLine("不是合法日期");
}
Console.ReadLine();
}
}
如果是web的话
那么可以这么写
protected void Page_Load()
{
string mystring = "2007年1月1日";
try
{
DateTime mydate = Convert.ToDateTime(mystring);
Response.Write(mydate);
}
catch
{
Response.Write("不是合法日期");
}
}
共同学习!
温馨提示:内容为网友见解,仅供参考