c# – 解析日期时间,但是dt正在将日期从8日改为7日?

DateTime dt = DateTime.Parse(value)

我的价值= {3/8/2011 12:00:00 AM}

但是dt显示dt = {3/7/2011 12:00:00 AM}

当我要拉我的头发时,请说清楚.

编辑:代码OP发布评论:

foreach (SPField field in contentType.Fields)
{
    string fValue;
    object value = spitem[field.Id];
    if (value is DateTime)
    {
        DateTime dateField = DateTime.Parse(field.GetFieldValueAsHtml(value));
        DateTime dt = DateTime.Parse(field.GetFieldValueAsText(value), CultureInfo.GetCultureInfo("en-US"));
        fValue = dt.ToShortDateString();
        lblMetaData.Text += field + ": " + fValue + "\r\n";
    }
    else
    {
        fValue = field.GetFieldValueForEdit(value);
        lblMetaData.Text += field + ": " + fValue + "\r\n";
    }
}
最佳答案
我的直觉告诉我代码中有一个拼写错误.可能缺少任务.

DateTime dt = DateTime.Parse("3/7/2011 12:00:00 AM");
....
DateTime.Parse("3/8/2011 12:00:00 AM"); //Parse's return is being ignored
....
dt is still {3/7/2011 12:00:00 AM}

确保调用DateTime.Parse(“3/8/2011 12:00:00 AM”);被分配给dt.

根据您的编辑,我觉得您的代码会更好,但是发布的代码仍然有用.

foreach (SPField field in contentType.Fields) 
{ 
    string fValue;
    object value = spitem[field.Id]; 

    if (value is DateTime) 
    { 
        DateTime dt = (DateTime)value;
        fValue = dt.ToShortDateString(); 
        lblMetaData.Text += field + ": " + fValue + "\r\n";
    } 
    else 
    {         
        fValue = field.GetFieldValueForEdit(value); 
        lblMetaData.Text += field + ": " + fValue + "\r\n"; 
    }  
}

转载注明原文:c# – 解析日期时间,但是dt正在将日期从8日改为7日? - 代码日志