简体   繁体   English

转换日期格式代码到目前为止

[英]Convert Date Formatting Codes to date

The user is supposed to enter date in format: %m %d %Y 用户应该以格式输入日期: %m %d %Y

What I need to do is convert the date to: 11 11 2013 ( which is today`s date). 我需要做的是将日期转换为: 11 11 2013 (今天的日期)。 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. 我浏览了DateTime选项但找不到我需要的东西。

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 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” %m - 月 - “01”到“12”

%d - day of the month, 2 digits with leading zeros - “01” to “31” %d - 月中的某天,前导零的2位数 - “01”到“31”

%Y - year, 4 digits - “1999” %Y - 年,4位数 - “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). 我想知道是否有一种方法将%m %d %Y作为输入并以指定的格式返回相应的今天日期(今天是11 11 2013 )。 Or at least something close to that. 或者至少接近那个。 Hope it is more clear now. 希望现在更清楚了。

EDIT 2: 编辑2:

After digging a little bit more I found that what I am looking for is an equivalent of C++ strftime in C#. 在进一步挖掘之后,我发现我正在寻找的东西相当于C#中的C ++ strftime。

http://www.cplusplus.com/reference/ctime/strftime/ http://www.cplusplus.com/reference/ctime/strftime/

But for some reason I cannot see an example this to implemented in C#. 但由于某些原因,我无法在C#中看到这样的示例。

You can use DateTime.TryParseExact to parse a string to date and DateTime-ToString to convert it back to string with your desired format: 您可以使用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);
}

My go-tos for DateTime Input and Output: 我对DateTime输入和输出的了解:

http://www.dotnetperls.com/datetime-parse for input (parsing) http://www.dotnetperls.com/datetime-parse用于输入(解析)

http://www.csharp-examples.net/string-format-datetime/ for output (formatting) 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);

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? 所以,如果我想看月,日,年,我说“MM dd YY”,你就把它还给我了?

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. 不过,你的问题仍然不太清楚。

使用ParseExact

var date = DateTime.ParseExact("9 1 2009", "M d yyyy", CultureInfo.InvariantCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM