从开始日期开始的剩余天数显示不正确

时间:2013-05-26 15:26:22

标签: c# asp.net

我有一个转发器,显示用户想要销售的商品。在转发器的一列中,CreateDate或项目发布的日期显示如下:

        //lblExp.Text originally contains 2013-05-24 14:24:08.000

        Label lblExp = (Label)e.Item.FindControl("lblExp");
        DateTime StartDate = Convert.ToDateTime(lblExp.Text);

        DateTime expDate = StartDate.AddDays(30);
        int DaysLeft = (expDate - StartDate).Days;
        lblExp.Text = DaysLeft.ToString();

当代码执行时,我得到的是30而不是28。

1 个答案:

答案 0 :(得分:2)

这是你想说的吗? (将StartDate替换为DateTime.Now

    //lblExp.Text originally contains 2013-05-24 14:24:08.000

    Label lblExp = (Label)e.Item.FindControl("lblExp");
    DateTime StartDate = Convert.ToDateTime(lblExp.Text);

    DateTime expDate = StartDate.AddDays(30);
    int DaysLeft = (expDate - DateTime.Now).Days; // Replaced here
    lblExp.Text = DaysLeft.ToString();