将日期格式代码转换为日期 [英] Convert Date Formatting Codes to date
问题描述
用户应该以格式输入日期:%m%d%Y
我需要什么要做的是将日期转换为: 11 11 2013
(这是今天的日期)。我没有太多工作日期。有没有一些方法可以将此转换开箱即用?我查看了DateTime选项,但找不到我需要的。
编辑:
从收到的答案看来,我不太清楚我在问什么。 / p>
在我们的软件中,用户可以使用以下格式插入日期:
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
)。或至少接近那个东西。
希望现在更清楚。
编辑2:
再挖一点点我发现我正在寻找的是相当于C#中的C ++ strftime。
http://www.cplusplus.com/reference/ctime/strftime/
但由于某种原因无法看到在C#中实现的一个例子。
我的DateTime输入和输出:
http://www.dotnetperls.com/datetime-parse for input(parsing)
http://www.csharp-examples.net/string-format-datetime/ 输出(格式)
string dateString =01 01 1992;
string format =MM dd yyyy;
DateTime dateTime = DateTime.ParseExact(dateString,format,CultureInfo.InvariantCulture);
自编辑以来编辑使我的上述答案无关(但会留下参考):
从你所说的,你想以动态定义的格式输出今天的日期?
所以如果我想看看月,日,年,我说MM dd YY,你还给我吗?
如果是的话:
DateTime dt = DateTime.Today; //或者像以前一样初始化它,并且解析(但是只是一个常规的DateTime dt = DateTime.Parse()或类似的东西)
然后
String formatString =MM dd YY;
String.Format({0:+ formatString +},dt);
尽管如此,您的问题仍不清楚。
The user is supposed to enter date in format: %m %d %Y
What I need to do is convert the date to: 11 11 2013
( which is today`s date). I have not worked much with dates. Is there some method that does this conversion out of the box? I looked through DateTime options but couldn't find what I need.
Edit:
From the answers received it seems that it is not very clear what I am asking.
In our software the user can insert dates in format like this:
http://ellislab.com/expressionengine/user-guide/templates/date_variable_formatting.html
I am trying to parse this user input and return the today date. So from the link above:
%m - month - "01" to "12"
%d - day of the month, 2 digits with leading zeros - "01" to "31"
%Y - year, 4 digits - "1999"
I was wondering if there is a method that takes %m %d %Y
as an input and returns the corresponding today date in the specified format ( which is 11 11 2013
today). Or at least something close to that.
Hope it is more clear now.
EDIT 2:
After digging a little bit more I found that what I am looking for is an equivalent of C++ strftime in C#.
http://www.cplusplus.com/reference/ctime/strftime/
But for some reason I cannot see an example this to implemented in C#.
My go-tos for DateTime Input and Output:
http://www.dotnetperls.com/datetime-parse for input (parsing)
http://www.csharp-examples.net/string-format-datetime/ for output (formatting)
string dateString = "01 01 1992";
string format = "MM dd yyyy";
DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Edit since his edit makes my above answer irrelevant (but will leave there for reference):
From what you're saying, you want to output today's date in a dynamically-defined format?
So if I want to see month, date, year, I say "MM dd YY" and you return it to me?
If so:
DateTime dt = DateTime.Today; // or initialize it as before, with the parsing (but just a regular DateTime dt = DateTime.Parse() or something quite similar)
Then
String formatString = "MM dd YY";
String.Format("{0:"+ formatString+"}", dt);
Your question is still quite unclear, though.
这篇关于将日期格式代码转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!