将Datetime转换为简单日期

时间:2013-09-18 11:16:33

标签: c# .net salesforce datetime-format date-conversion

我无法将String格式的变量(从表单上的DateTime选择器控件)转换为简单的Date。

我正在尝试转换,因为我收到以下错误消息:

  

INVALID_FIELD:\ nAND c.Status__c ='已关闭'和c.Close_Date__c>   2013-07-01T00:00:00Z \ n ^ \ nERROR at Row:1:Column:326 \ nvalval of filter   字段'Close_Date__c'的标准必须是date和should的类型   不要用引号括起来

我试过了:

sQry += " AND c.Close_Date__c > " + DateTime.ParseExact(filterFromDate, "d", CultureInfo.InvariantCulture) + " AND c.Close_Date__c < " + DateTime.ParseExact(filterFromDate, "d", CultureInfo.InvariantCulture);

但它不接受它(编译器无法识别CultureInfo

我哪里出错了?

更多信息: 对不起,我应该提一下,我们对引用SalesForce实例的SalesForce对象使用此查询

2 个答案:

答案 0 :(得分:1)

添加System.Globalization然后......

var date = DateTime.ParseExact(filterFromDate, "dd", CultureInfo.InvariantCulture);

答案 1 :(得分:1)

...更新

那是怎么回事(我做了很多假设.....) 哪里

string filterFromDate = "2013-07-01T00:00:00Z";

sQry += " AND c.Close_Date__c > " + DateTime.Parse(filterFromDate).Date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'sszzzzz") + " AND c.Close_Date__c < " + DateTime.Parse(filterFromDate).Date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'sszzzzz");

<击> 或者更好的使用SqlParameters

string filterFromDateStr = "2013-07-01T00:00:00Z";

//Just the date since we don't want to use the Time
var filterFromDate = DateTime.Parse(filterFromDateStr ).Date;

sQry += " AND c.Close_Date__c > @filterFromDate AND c.Close_Date__c < @filterFromDate";

IDbCommand cmd = connection.CreateCommand();
cmd.CommandTimeout = connection.ConnectionTimeout;
cmd.CommandText = sQuery;

cmd.Parameters.Add(new SqlParameter("@filterFromDate ",filterFromDate ));

md.Connection = connection;
var reader = cmd.ExecuteReader();

<击>