使用C#制作算法来理解00/00/00

时间:2014-12-09 23:45:00

标签: c#

我正在为名为Prophet 21的程序编写业务规则的C#代码。

该代码基本上是说,如果产品价格在去年更新,请将该价格放入名为PO Price的字段中。

除了一个小问题外,一切都很有效,当确保价格在去年内更新时,它会查看PO价格更新日期。在过去的某个时刻,很多这些设置为00/00/00并且使用带日期的算法似乎不理解它。我最好的猜测是我需要以某种方式转换日期,但我是一个C#新手,任何帮助将不胜感激。这是我正在使用的代码。

public class SetPOCost : Rule  //this rule sets the PO cost to the other cost when the item is stockable and the disposition is B
{
    public override RuleResult Execute()
    {
        RuleResult result = new RuleResult();

        result.Success = true;

        string s = Data.Fields["other_cost"].FieldValue;
        decimal ldcOtherCost = Convert.ToDecimal(s);
        s = Data.Fields["po_cost"].FieldValue;
        decimal ldcPOCost = Convert.ToDecimal(s);
        string stockable = Data.Fields["stockable"].FieldValue;
        string disposition = Data.Fields["disposition"].FieldValue;
        s = Data.Fields["ufc_inventory_supplier_date_cost_last_modified"].FieldValue;
        //this next line is added from the SuggestPromiseDate rule because it was running into errors doing both rules
        Data.Fields.GetFieldByAlias("suggested_promise_date").FieldValue = Data.Fields.GetFieldByAlias("line_max_promise_date").FieldValue;
        DateTime dtLastModified = Convert.ToDateTime(s);

        DateTime thisDate = DateTime.Today;
        DateTime thisDateLastYear = thisDate.AddYears(-1);
        Boolean dateIsGood = dtLastModified.CompareTo(thisDateLastYear) >= 0;

        if (stockable == "N" && disposition == "B" && ldcPOCost == 0 && ldcOtherCost > 0 && dateIsGood)
        {
            Data.Fields["po_cost"].FieldValue = ldcOtherCost.ToString();
        }

        return result;
    }

3 个答案:

答案 0 :(得分:1)

您可以使用TryParse检查即将到来的日期。

s = Data.Fields["ufc_inventory_supplier_date_cost_last_modified"].FieldValue;
//this next line is added from the SuggestPromiseDate rule because it was running into errors doing both rules
Data.Fields.GetFieldByAlias("suggested_promise_date").FieldValue = Data.Fields.GetFieldByAlias("line_max_promise_date").FieldValue;

DateTime dtLastModified;
if (DateTime.TryParse(s, out dtLastModified) == false)
    dtLastModified = DateTime.MinValue;

答案 1 :(得分:1)

如何:

s = Data.Fields["ufc_inventory_supplier_date_cost_last_modified"].FieldValue;
if (s == "00/00/00") {
    s="01/01/0001";
}

答案 2 :(得分:0)

00/00/00不是.NET中的有效日期;日期从0001年1月1日开始计算。检测此日期何时出现在原始数据源中,然后创建新日期new DateTime(0);