如何验证日期和迄今 [英] How to validate from date and To date

查看:58
本文介绍了如何验证日期和迄今的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在进行日期和迄今为止的验证。如何确定日期是否应该超过日期。



谢谢和问候,

MGBalamurugan

Hi,
I'm working on from date and To date validation.How to chk whether to date should be more than from date.

Thanks and Regards,
M.G.Balamurugan

推荐答案

不仅仅是:
d1 = DateTime.Now;
d2 = DateTime.Now.AddDays(12);

if (d2 > d1)
{
    // date order ok
}
else
{
   // date order reversed
   // swap the dates ?
}

你想要吗?



你想在两个日期之间指定最小和/或最大间隔(TimeSpan)吗?



你想要一个非常具体的DateTime验证器,其中标准是固定的,或者你想要一个通用的验证器,你可以将参数传递到处理各种条件吗?



如果将某些日期间隔包括在任何周末,或任何特定的一天中的小时或晚上,如何排除这些日期间隔?



让我们看一些你的鳕鱼e。

Do you want ?

Do you want to specify minimum and/or maximum intervals (TimeSpan) between the two Dates ?

Do you want a very specific DateTime validator where the criteria are fixed, or do you want a general purpose validator you can pass parameters into that handle a variety of conditions ?

How about excluding certain Date intervals if they include any weekend day, or any specific range of hours of day, or night ?

Let's see some of your code.


使用 DateTime.TryParse方法 [ ^ ]并比较它们。这就是全部!
Get date from controls using DateTime.TryParse method[^] and compare them. That's all!


下面的方法应该在按钮验证中显示,

The below method should show in button validation,
string valid = CheckValid();
if (valid == "")
{ 
    errormsg.Text = valid;
}





方法应该在这里定义



The method should define here

private string CheckValid()
{
    try
    {
        string valid;
        if (txtFromDate.Text != string.Empty && txtToDate.Text != string.Empty)
        {
            DateTime startdate = DateTime.Parse(txtFromDate.Text);
            DateTime enddate = DateTime.Parse(txtToDate.Text);
            if (startdate > enddate)
            {
                txtFromDate.Focus();
                return valid = "From Date is Should be less than To Date";
            }
            else
            {

            }
        }

        return valid = "";
    }
    catch (Exception Ex)
    {
        throw (Ex);
    }
}


这篇关于如何验证日期和迄今的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆