如果声明 - 日期将在45天后到期 - [英] If statement -- date will expire in 45 days --

查看:117
本文介绍了如果声明 - 日期将在45天后到期 - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个带有col日期的表,这个日期有if语句检查日期是否会在45显示此行,如果不是不排但有些如何不工作可以有人帮助或建议新的答案。



我尝试了什么:



hello i have table with col date and this date has if statement to check if the date will expire in 45 show this row, if not don't row but some how this is not working can someone help or suggest new answer .

What I have tried:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label b = e.Row.FindControl("Label27") as Label;

        if (b != null)
        {
            DateTime lblDate;
            if (!DateTime.TryParse(b.Text, out lblDate))
            {
                // date time conversion not success 
                // you may have empty or invalid datetime 
                // do something in this case 
                return;
            }

            if (lblDate <= DateTime.Today.AddDays(45))
            {

                e.Row.Visible = true;

            }

            else
            {
                e.Row.Visible = false;

            }


        }
    }

推荐答案

试试这个

try this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           Label lblDate = e.Row.FindControl("Label27") as Label;
           DateTime date; string yourDateFormat = "MM/dd/yyyy"; // check the label date format
           if (lblDate != null)
               if (!string.IsNullOrEmpty(lblDate.Text)) // check is null or empty
                   if (DateTime.TryParseExact(lblDate.Text, yourDateFormat, CultureInfo.CurrentCulture, DateTimeStyles.None, out date))
                   {
                       bool show = date <= DateTime.Today.AddDays(45);
                       e.Row.Visible = show;
                   }
       }



使用DateTime.TryParseExact Method [ ^ ]将字符串转换为日期时间。


use DateTime.TryParseExact Method [^] to convert string to datetime.


这篇关于如果声明 - 日期将在45天后到期 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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