将字符串转换为日期时间格式c#时出错

时间:2018-03-27 02:56:38

标签: c# datetime-format

我无法将字符串转换为日期时间。

以下是代码:

string[] itemIDList = null,
brandIdList = null,
indentItemIdList = null,
qtyList = null,
requiredDateList = null;

itemIDList = form["ItemId"].Split(',');
brandIdList = form["BrandId"].Split(',');
indentItemIdList = form["IndentItemId"].Split(',');
qtyList = form["Qty"].Split(',');
requiredDateList = form["RequiredDate"].Split(',');

//int fId = 0;
//selected fields
int cSelCount = 0;
if (itemIDList != null)
{
    foreach (string itemField in itemIDList)
    {
        string indentitemId = indentItemIdList[cSelCount] == "" 
            ? "0" 
            : indentItemIdList[cSelCount];

        string brandField = brandIdList[cSelCount];
        string qtyField = qtyList[cSelCount];

        DateTime requiredDateFiled = requiredDateList[cSelCount] == "" 
            ? DateTime.Now 
            : DateTime.ParseExact(requiredDateList[cSelCount], "dd-MM-yyyy", 
                CultureInfo.InvariantCulture);

        if (!string.IsNullOrEmpty(itemField) && 
            !string.IsNullOrEmpty(brandField) && 
            !string.IsNullOrEmpty(qtyField) && 
            qtyField != "0" && qtyField != "")
        {
            IndentItemModel objIndentItem = new IndentItemModel();
            objIndentItem.IndentItemId = Convert.ToInt32(indentitemId);
            objIndentItem.ItemId = Convert.ToInt32(itemField);
            objIndentItem.BrandId = Convert.ToInt32(brandField);
            objIndentItem.Qty = Convert.ToInt32(qtyField);
            objIndentItem.ItemStatusId = Convert.ToInt32(ItemStatus.Indent);
            objIndentItem.RequiredDate = Convert.ToDateTime(requiredDateFiled);
            objIndentItem.CreatedBy = objIndentItem.ModifiedBy = objModel.ModifiedBy;
            objIndentItem.ModifiedDate = objIndentItem.CreatedDate = DateTime.Now;
            indentItemList.Add(objIndentItem);
        }

        cSelCount++;
    }
}

错误讯息:

Error message

相同的格式化代码在foreach循环之外工作..我希望这与foreach循环无关,但有些我错过了一些东西..请帮我解决

changed my code to this format but still had the error

我的系统日期foramt是mm-dd-yy,我正在解析为dd-MM-yyyy ..

2 个答案:

答案 0 :(得分:0)

我尝试重新生成代码而没有错误。 requiredDateFiledDateTime,因此您可以直接使用它。

string[] requiredDateList = new string[] { "03-27-2018", "" };
string[] itemIDList = new string[] { "1", "2" };

int cSelCount = 0;
if (itemIDList != null)
{
   foreach (string itemField in itemIDList)
   {
       DateTime requiredDateFiled = requiredDateList[cSelCount] == "" ? DateTime.Now : DateTime.ParseExact(requiredDateList[cSelCount].ToString(),
                        "MM-dd-yyyy", CultureInfo.InvariantCulture);

       // THIS LINE IS FOR KNOWLEDGE ONLY NO NEED OF THIS.
       requiredDateFiled = DateTime.ParseExact(requiredDateFiled.ToString("dd-MM-yyyy"), "dd-MM-yyyy", CultureInfo.InvariantCulture);
    }
}

答案 1 :(得分:-2)

CultureInfo provider = new CultureInfo("nl-NL");
DateTime dt = String.IsNullOrEmpty(date) ? DateTime.Now : DateTime.Parse(date);
Console.WriteLine(date.ToString("d", provider));

试试这个。