怎么样判断一个数据是不是DateTime 类型的 try{Convert.ToDateTime();}}catch{} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这样来做:object tmpObj = new DateTime(2005,2,2);if ( tmpObj is DateTime){ Console.WriteLine("tmpObj 是DateTime类型的数据");} try{DataTime edate = DateTime.Parse(yourData);}catch{} 用是用is运算符:is 运算符用于检查对象的运行时类型是否与给定类型兼容。 用isif (obj is DateTime){ (obj as DattTime).xxxxx} using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace DateTimePra{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(104, 176); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(80, 80); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = "textBox1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { try { DateTime dt = Convert.ToDateTime(this.textBox1.Text); MessageBox.Show(dt.ToString()); } catch(Exception ex) { MessageBox.Show(ex.Message); } } }} 假设要判断的字符串为 sif ( s is DateTime){ .....} 我总结一下。如果你仅仅需要判断这个数据是否为DateTime, 可以用is.但是如果他是一个string,你需要用到Parse。 大家帮帮忙 打开了新窗体,如何才能在不退出该窗体的情况下操作原来的窗体? 有人使用ZPL编写条码打印机程序吗?EAN128的打印如何实现? 调用事件 关于ASP.NET与J2EE的一点问题 如何在HttpHandler中为页面增加Request参数 怎么判断数字 如何能将aceess里面的数据倒入到listview中? VS2005中dataGridView1行为什么不能删除? 100分求:怎样使QQ下线、上线、隐身等???? 请看看这里Poly2(Object myClass)的执行结果为什么会不反映多态,能否解释一下?谢谢! 求多种解法
if ( tmpObj is DateTime)
{
Console.WriteLine("tmpObj 是DateTime类型的数据");
}
{
DataTime edate = DateTime.Parse(yourData);}
catch
{}
if (obj is DateTime)
{
(obj as DattTime).xxxxx
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace DateTimePra
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 80);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
try
{
DateTime dt = Convert.ToDateTime(this.textBox1.Text);
MessageBox.Show(dt.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
{
.....
}