C#日期比较功能产生错误的结果

时间:2018-10-10 14:22:48

标签: c# sql-server webforms

我有DueDat的两列PaymentDatedatagridview。我要那个 如果付款已到beforeONafter到期,则结果应为Paid。比较两个日期列时我需要以下结果

今天的日期是10-10-2018

InstallmentNo    DueDate    PaymentDate    Status
-------------------------------------------------    
1              08-11-2018   18-11-2018      Paid
2              08-12-2018                   Up Coming
3              08-12-2018                   Up Coming
4              08-12-2018                   Up Coming

InstallmentNo    DueDate    PaymentDate    Status
---------------------------------------------------        
    1            27-10-2018               Up Coming
    2            08-12-2018               Up Coming
    3            08-12-2018               Up Coming
    4            08-12-2018               Up Coming

但是我的代码产生以下结果:

InstallmentNo    DueDate    PaymentDate    Status
-------------------------------------------------      
1              08-11-2018   18-11-2018      Paid
2              08-12-2018                   Pending
3              08-12-2018                   Up Coming
4              08-12-2018                   Up Coming

InstallmentNo    DueDate    PaymentDate    Status
--------------------------------------------------        
    1            27-10-2018               Pending
    2            08-12-2018               Pending
    3            08-12-2018               Pending
    4            08-12-2018               Pending

这是我的代码,该代码从textbox输入上述表格数据

private void txtSID_TextChanged(object sender, EventArgs e)
{
    try
    {
        int isid;
        string sid = "";

        sid = txtSID.Text.ToString();

        if (int.TryParse(sid, out isid)) ;

        using (SqlConnection conn = new SqlConnection(connection))
        {
            string CmdString = " SELECT InsttNo,CONVERT(VARCHAR(10),DD,105) as DD,CONVERT(VARCHAR(10),PD,105) as PD from InstallmentPaymentHistory where SalesInvoiceID=" + isid + "";

            SqlCommand cmd = new SqlCommand(CmdString, conn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt1 = new DataTable("SalesInvoice");
            sda.Fill(dt1);
            dataGridView1.DataSource = dt1.DefaultView;

            DateTime d1 = new DateTime();
            DateTime d2 = new DateTime();
            DateTime d3 = new DateTime(2011, 2, 19);

            d3 = DateTime.Now;
            string s = d3.ToString("dd-MM-yyyy");

            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                var dueDate = dataGridView1.Rows[i].Cells["DueDate"].Value != null
                    ? dataGridView1.Rows[i].Cells["DueDate"].Value.ToString()
                    : string.Empty;

                var paymentDate = dataGridView1.Rows[i].Cells["PaymentDate"].Value != null
                    ? dataGridView1.Rows[i].Cells["PaymentDate"].Value.ToString()
                    : string.Empty;

                if (!DateTime.TryParse(dueDate, out d1) || !DateTime.TryParse(paymentDate, out d2)) ;

                int a = DateTime.Compare(d1, d2);

                int b = DateTime.Compare(d3, d1);

                if ((a <= 0 || a >= 0) && paymentDate.ToString() != "")
                    dataGridView1.Rows[i].Cells["Status"].Value = "Paid";
                else if (b <= 0)
                    dataGridView1.Rows[i].Cells["Status"].Value = "Up Coming";
                else
                    dataGridView1.Rows[i].Cells["Status"].Value = "Pending";
            }
        }
    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.ToString());
    }
}

1 个答案:

答案 0 :(得分:0)

免责声明:我在英国,解析格式为“ dd-MM-yyyy”的字符串对于DateTime.Parse()DateTime.TryParse()来说可以正常工作。在其他文化中可能无法使用。

日期解析似乎存在问题,您应该使用DateTime.TryParseExact()来最大程度地减少文化差异带来的问题。

您还应该避免使用DateTime.Compare(),这只会使问题感到困惑。

var dueDateString = "08-11-2018";
var paymentDateString = "18-11-2018";

DateTime dueDate;
DateTime paymentDate;

DateTime.TryParseExact(
    dueDateString,
    "dd-MM-yyyy",
    System.Globalization.CultureInfo.InvariantCulture, 
    System.Globalization.DateTimeStyles.AdjustToUniversal,
    out dueDate);

DateTime.TryParseExact(
    paymentDateString,
    "dd-MM-yyyy",
    System.Globalization.CultureInfo.InvariantCulture, 
    System.Globalization.DateTimeStyles.AdjustToUniversal,
    out paymentDate);

if (paymentDate != default(DateTime)) // valid payment date
    dataGridView1.Rows[i].Cells["Status"].Value = "Paid";
else if (dueDate > DateTime.Today) // due date is in the future
    dataGridView1.Rows[i].Cells["Status"].Value = "Up Coming";
else // payment is overdue
    dataGridView1.Rows[i].Cells["Status"].Value = "Pending";