public CrmDateTime CRMFormattedDate(string dateValue)
{
DateTime cd = new DateTime();
string date_str = "";
try
{
if (!isValidYear(dateValue.Substring(6, 4)))
date_str = DateTime.Now.Year.ToString();
else
date_str = dateValue.Substring(6, 4);
cd.Value = date_str + "/" + dateValue.Substring(3, 2) + "/" + dateValue.Substring(0, 2) + "T00:00:00";
}
catch (Exception ex)
{
cd = null;
}
return cd;
}
上面的代码会引发错误。我试图解决这个问题,但没有任何运气。有人可以帮我吗?
答案 0 :(得分:0)
public CrmDateTime CRMFormattedDate(string dateValue)
{
CrmDateTime cd = new CrmDateTime();
string date_str = "";
try
{
if (!isValidYear(dateValue.Substring(6, 4)))
date_str = DateTime.Now.Year.ToString();
else
date_str = dateValue.Substring(6, 4);
cd.Value = date_str + "/" + dateValue.Substring(3, 2) + "/" + dateValue.Substring(0, 2) + "T00:00:00";
}
catch (Exception ex)
{
cd = null;
}
return cd;
}
private bool isValidYear(string year_value)
{
if (Convert.ToInt16(year_value) < 1800)
return false;
return true;
}
请试一试。
答案 1 :(得分:0)
也许这种方法可以帮助您,所有这些都是从Microsoft.Crm.Platform.Sdk.dll文件中提取的。
public static CrmDateTime FromString(string serializedForm, string formattedDate, string formattedTime)
{
CrmDateTime crmDateTime = new CrmDateTime();
crmDateTime.Deserialize(serializedForm, formattedDate, formattedTime);
return crmDateTime;
}
public override string ToString()
{
switch (this._style)
{
case CrmDateTime.Style.None:
{
return "Uninitialized";
}
case CrmDateTime.Style.User:
{
CultureInfo invariantCulture = CultureInfo.InvariantCulture;
object[] objArray = new object[] { this._userTime };
return string.Format(invariantCulture, "(user:{0:s})", objArray);
}
case CrmDateTime.Style.Universal:
{
CultureInfo cultureInfo = CultureInfo.InvariantCulture;
object[] objArray1 = new object[] { this._universalTime };
return string.Format(cultureInfo, "(universal:{0:s})", objArray1);
}
case CrmDateTime.Style.Both:
{
CultureInfo invariantCulture1 = CultureInfo.InvariantCulture;
object[] objArray2 = new object[] { this._userTime, this._universalTime };
return string.Format(invariantCulture1, "(user:{0:s}, universal:{1:s})", objArray2);
}
}
CultureInfo cultureInfo1 = CultureInfo.InvariantCulture;
object[] str = new object[] { this._style.ToString() };
throw new InvalidOperationException(string.Format(cultureInfo1, "Invalid CrmDateTime style: {0}", str));
}
private void Deserialize(string serializedForm, string formattedDate, string formattedTime)
{
DateTime dateTime;
string str = serializedForm.Trim();
this._formattedDate = formattedDate;
this._formattedTime = formattedTime;
int length = str.Length;
if (length == 0)
{
throw new CrmArgumentNullException("Empty string is not a valid representation of CrmDateTime.");
}
if (!DateTime.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dateTime))
{
CultureInfo invariantCulture = CultureInfo.InvariantCulture;
object[] objArray = new object[] { str };
throw new CrmException(string.Format(invariantCulture, "The date-time format for {0} is invalid, or value is outside the supported range.", objArray), -2147220935);
}
if (str[length - 1] == 'Z' || str[length - 1] == 'z')
{
this.Initialize(CrmDateTime.Style.Universal, dateTime);
return;
}
if (length > 6 && (str[length - 6] == '+' || str[length - 6] == '-') && str[length - 3] == ':' && str.IndexOf(':', 0, length - 6) >= 0)
{
string str1 = str.Substring(0, length - 6);
DateTime dateTime1 = DateTime.Parse(str1, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
this.Initialize(dateTime1, dateTime);
return;
}
DateTime dateTime2 = dateTime;
if (!SerializationState.IsInPlatformContext)
{
this.Initialize(CrmDateTime.Style.User, dateTime2);
return;
}
DateTime utcTime = SerializationState.ConvertUserTimeToUtcTime(dateTime2);
this.Initialize(dateTime2, CrmDateTime.AdjustToLimits(utcTime));
}
public string Serialize()
{
string str;
CrmDateTime.Style style = this._style;
DateTime userTime = this._userTime;
if (SerializationState.IsInPlatformContext)
{
switch (style)
{
case CrmDateTime.Style.User:
{
throw new InvalidOperationException("CrmDateTime on the server must provide date-time in universal time zone. Current value passed contains time in the user time zone only.");
}
case CrmDateTime.Style.Universal:
{
userTime = SerializationState.ConvertUtcTimeToUserTime(this._universalTime);
userTime = CrmDateTime.AdjustToLimits(userTime);
style = CrmDateTime.Style.Both;
break;
}
}
}
switch (style)
{
case CrmDateTime.Style.User:
{
CultureInfo invariantCulture = CultureInfo.InvariantCulture;
object[] objArray = new object[] { userTime };
return string.Format(invariantCulture, "{0:s}", objArray);
}
case CrmDateTime.Style.Universal:
{
CultureInfo cultureInfo = CultureInfo.InvariantCulture;
object[] objArray1 = new object[] { this._universalTime };
return string.Format(cultureInfo, "{0:s}Z", objArray1);
}
case CrmDateTime.Style.Both:
{
TimeSpan timeSpan = userTime - this._universalTime;
CrmException.Assert((timeSpan.TotalDays < -1 ? false : timeSpan.TotalDays <= 1), "User and universal time should not be further than 1 day away.");
if (timeSpan.Hours <= 0)
{
CultureInfo invariantCulture1 = CultureInfo.InvariantCulture;
object[] hours = new object[] { userTime, -timeSpan.Hours, -timeSpan.Minutes };
str = string.Format(invariantCulture1, "{0:s}-{1:00}:{2:00}", hours);
}
else
{
CultureInfo cultureInfo1 = CultureInfo.InvariantCulture;
object[] hours1 = new object[] { userTime, timeSpan.Hours, timeSpan.Minutes };
str = string.Format(cultureInfo1, "{0:s}+{1:00}:{2:00}", hours1);
}
return str;
}
}
CultureInfo invariantCulture2 = CultureInfo.InvariantCulture;
object[] str1 = new object[] { style.ToString() };
throw new InvalidOperationException(string.Format(invariantCulture2, "Invalid CrmDateTime style: {0}", str1));
}