用户应该以以下格式输入日期:%m %d %Y
我需要做的是将日期转换为:11 11 2013
(这是今天的日期)。我没有太多处理日期的工作。有没有某种方法可以开箱即用地进行这种转换?我查看了 DateTime 选项,但找不到我需要的内容。
Edit:
从收到的答案来看,我的要求似乎不太清楚。
在我们的软件中,用户可以插入如下格式的日期:
http://ellislab.com/expressionengine/user-guide/templates/date_variable_formatting.html http://ellislab.com/expressionengine/user-guide/templates/date_variable_formatting.html
我正在尝试解析此用户输入并返回今天的日期。所以从上面的链接:
%m - 月份 - “01”到“12”
%d - 一个月中的第几天,带前导零的 2 位数字 - “01”到“31”
%Y - 年份,4 位数字 - “1999”
我想知道是否有一种方法可以%m %d %Y
作为输入并以指定格式返回相应的今天日期(即11 11 2013
今天)。或者至少是接近的东西。
希望现在更清楚了。
EDIT 2:
经过进一步挖掘,我发现我正在寻找的相当于 C# 中的 C++ strftime。
http://www.cplusplus.com/reference/ctime/strftime/ http://www.cplusplus.com/reference/ctime/strftime/
但由于某种原因,我看不到在 C# 中实现的示例。
您可以使用DateTime.TryParseExact
解析一个字符串到日期和DateTime-ToString
将其转换回您想要的格式的字符串:
DateTime parsedDate;
if (DateTime.TryParseExact("11 11 2013", "MM dd yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out parsedDate))
{
// parsed successfully, parsedDate is initialized
string result = parsedDate.ToString("MM dd yyyy", System.Globalization.CultureInfo.InvariantCulture);
Console.Write(result);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)