一个字符串怎么可以校验它可不可以转化为日期型
比如string tt="2005-5-18";就可以转化
而string tt="2005-5-58";就不可以转化.
if(inputdate.Value is DateTime)
{
//do something;
}
else
{
return;
}
比如string tt="2005-5-18";就可以转化
而string tt="2005-5-58";就不可以转化.
if(inputdate.Value is DateTime)
{
//do something;
}
else
{
return;
}
解决方案 »
- 数据库配置和程序配置
- 高分在线求如何获取网页代码
- 如何用C#编程保存一个网页到本地文件夹下
- 一个类前面加了XmlRoot特性,序列化后的xml如何才能不带有任何命名空间
- 有没有做出漂亮界面的组件,DevExpress是个什么组件?从那里下载
- 如何得到计算机的所有用户?
- [问题] 如何在远程机器上新建一个文件夹、一个文件;并写这个文件。
- .net framework?
- 请问:为什么我不能using system.data.sql
- WinForm,一个复合控件,由groupbox和radiobutton组成,无法显示radiobutton
- 请大家帮个忙。分数好说。
- 关于全文检索的问题,很有挑战性的阿
Convert.ToDateTime(tt)
}
catch(Exception)
{}
debug模式:34.7毫秒
release模式:2.8679毫秒
这个时间不是很长嘛,这里不是和 闵峰 抬杠,请不要生气。
我的意思,大家说的用正则是好方法,但是不要都一概认为try,catch速度慢,在编译环境,用debug模式,调试运行,是慢,编译后,非调试模式,直接运行,速度很快的。
private void button1_Click(object sender, System.EventArgs e)
{
string str = "aaa";
CountFunction.CountFunction ct = new CountFunction.CountFunction();
try
{
ct.Start();
DateTime.Parse(str);
}
catch
{
ct.Stop();
MessageBox.Show((ct.Duration*1000).ToString());
}
}
计算时间的类库:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;
namespace CountFunction
{
public class CountFunction
{#region DllImport
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency); [DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
#endregion#region Declare Variable
private long startTime,stopTime;
private long freq;
#endregion#region Function
public CountFunction()
{
this.startTime = 0;
this.stopTime = 0;
if(QueryPerformanceFrequency(out freq) == false) throw new Win32Exception(); } // 在开始要记录时间的方法
public void Start()
{
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
} // 停止记录时间的方法
public void Stop()
{
QueryPerformanceCounter(out stopTime);
} // 获取时间差,得到函数执行时间
public double Duration
{
get
{
return (double)(stopTime - startTime)/(double)freq;
}
}#endregion
}
}