将字符串转换为日期格式? [英] Convert string to date format?
问题描述
如何将字符串转换为日期格式?
我尝试过:
公共的ActionResult SaveSelectedCustomers(串GetDateTime)
{
尝试
{
的DateTime getDateTime = DateTime.ParseExact(GetDateTime,年月日HH: mm,CultureInfo.InvariantCulture);
返回Json(true);
}
catch(exception ex)
{
throw ex;
}
}
它会抛出以下错误: -
oMail.Web.dll中出现'System.FormatException'类型的异常,但未在用户代码中处理
附加信息:字符串未被识别为一个有效的DateTime。
Maciej Los写道:
你确定字符串是那种格式:yyyyMMdd
?
Shridhar Salunkhe写道:
dateFormat:dd-mm-yy
in jquery
DateTime.ParseExact Method(String,String,IFormatProvider)(System) [ ^ ]要求精确日期的字符串表示形式,必须采用format参数定义的格式。如果有时间部分,你必须定义它。
如果您有这样的日期格式:2017年5月22日星期一12:48:00 GMT + 0530
,你必须使用 DateTimeOffset.ParseExact Method(String,String,IFormatProvider)(System) [ ^ ]而不是DateTime.Parse
方法。
请参阅: C#string to DateTime with timezone - 堆栈溢出 [ ^ ]
how to convert string into date format?
What I have tried:
public ActionResult SaveSelectedCustomers(string GetDateTime) { try { DateTime getDateTime = DateTime.ParseExact(GetDateTime, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture); return Json(true); } catch( Exception ex) { throw ex; } }
it throws following error:-
An exception of type 'System.FormatException' occurred in oMail.Web.dll but was not handled in user code Additional information: String was not recognized as a valid DateTime.
Maciej Los wrote:
Are you sure that string is in that format: "yyyyMMdd
"?
Shridhar Salunkhe wrote:
dateFormat: "dd-mm-yy
" in jquery
DateTime.ParseExact Method (String, String, IFormatProvider) (System)[^] expects a exact string representation of a date, which must be in the format defined by the format parameter. If there's a time part, you have to define it.
[EDIT]
In case you have a date format like this:Mon May 22 2017 12:48:00 GMT+0530
, you have to use DateTimeOffset.ParseExact Method (String, String, IFormatProvider) (System)[^] instead ofDateTime.Parse
method.
See: C# string to DateTime with timezone - Stack Overflow[^]
这篇关于将字符串转换为日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!