怎么样判断一个数据是不是DateTime 类型的

MScharlie 2005-02-02 03:41:14
谢谢
...全文
602 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
manonroad 2005-02-02
  • 打赏
  • 举报
回复 1
我总结一下。如果你仅仅需要判断这个数据是否为DateTime, 可以用is.但是如果他是一个string,你需要用到Parse。
meixiaofeng 2005-02-02
  • 打赏
  • 举报
回复 1
楼上说的好,回答问题就地简单明了
gsky 2005-02-02
  • 打赏
  • 举报
回复 1
假设要判断的字符串为 s

if ( s is DateTime)
{
.....
}
harisonh2l 2005-02-02
  • 打赏
  • 举报
回复
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);
}
}
}
}
liduke 2005-02-02
  • 打赏
  • 举报
回复
用is
if (obj is DateTime)
{
(obj as DattTime).xxxxx
}
北京的雾霾天 2005-02-02
  • 打赏
  • 举报
回复
用是用is运算符:

is 运算符用于检查对象的运行时类型是否与给定类型兼容。
manonroad 2005-02-02
  • 打赏
  • 举报
回复
try
{
DataTime edate = DateTime.Parse(yourData);

}
catch
{

}
北京的雾霾天 2005-02-02
  • 打赏
  • 举报
回复
这样来做:

object tmpObj = new DateTime(2005,2,2);
if ( tmpObj is DateTime)
{
Console.WriteLine("tmpObj 是DateTime类型的数据");
}
Edelweissobject 2005-02-02
  • 打赏
  • 举报
回复
try{
Convert.ToDateTime();}
}
catch{
}
LoveCherry 2005-02-02
  • 打赏
  • 举报
回复
try{Convert.ToDateTime();}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧