C# 将字符串格式转换为 mm/dd/yyyy 中的日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10043022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting string format to datetime in mm/dd/yyyy
提问by Aviral Kumar
I have to convert string in mm/dd/yyyy format to datetime variable but it should remain in mm/dd/yyyy format.
我必须将 mm/dd/yyyy 格式的字符串转换为 datetime 变量,但它应该保留为 mm/dd/yyyy 格式。
string strDate = DateTime.Now.ToString("MM/dd/yyyy");
Please help.
请帮忙。
采纳答案by Darbio
You are looking for the DateTime.Parse()method (MSDN Article)
您正在寻找DateTime.Parse()方法(MSDN 文章)
So you can do:
所以你可以这样做:
var dateTime = DateTime.Parse("01/01/2001");
Which will give you a DateTimetyped object.
这会给你一个DateTime类型化的对象。
If you need to specify which date format you want to use, you would use DateTime.ParseExact(MSDN Article)
如果您需要指定要使用的日期格式,您可以使用DateTime.ParseExact(MSDN 文章)
Which you would use in a situation like this (Where you are using a British style date format):
您将在这样的情况下使用(您使用的是英式日期格式):
string[] formats= { "dd/MM/yyyy" }
var dateTime = DateTime.ParseExact("01/01/2001", formats, new CultureInfo("en-US"), DateTimeStyles.None);
回答by chakrit
You need an uppercase Mfor the month part.
M月份部分需要大写。
string strDate = DateTime.Now.ToString("MM/dd/yyyy");
Lowercase mis for outputting (and parsing) a minute (such as h:mm).
小写m用于输出(和解析)一分钟(例如h:mm)。
e.g. a full date time string might look like this:
例如,完整的日期时间字符串可能如下所示:
string strDate = DateTime.Now.ToString("MM/dd/yyyy h:mm");
Notice the uppercase/lowercase mMdifference.
注意大写/小写的mM区别。
Also if you will always deal with the same datetime format string, you can make it easier by writing them as C# extension methods.
此外,如果您将始终处理相同的日期时间格式字符串,则可以通过将它们编写为 C# 扩展方法来使其更容易。
public static class DateTimeMyFormatExtensions
{
public static string ToMyFormatString(this DateTime dt)
{
return dt.ToString("MM/dd/yyyy");
}
}
public static class StringMyDateTimeFormatExtension
{
public static DateTime ParseMyFormatDateTime(this string s)
{
var culture = System.Globalization.CultureInfo.CurrentCulture;
return DateTime.ParseExact(s, "MM/dd/yyyy", culture);
}
}
EXAMPLE:Translating between DateTime/string
示例:在日期时间/字符串之间转换
DateTime now = DateTime.Now;
string strNow = now.ToMyFormatString();
DateTime nowAgain = strNow.ParseMyFormatDateTime();
Note that there is NOway to store a custom DateTimeformat information to use as defaultas in .NET most string formatting depends on the currently set culture, i.e.
请注意,有否办法自定义存储DateTime的格式信息,用作default在大多数.NET字符串格式化取决于当前集文化,即
System.Globalization.CultureInfo.CurrentCulture.
The only easy way you can do is to roll a custom extension method.
您可以做的唯一简单方法是推出自定义扩展方法。
Also, the other easy way would be to use a different "container" or "wrapper" class for your DateTime, i.e. some special class with explicit operatordefined that automatically translates to and from DateTime/string. But that is dangerous territory.
此外,另一种简单的方法是为您的 DateTime 使用不同的“容器”或“包装器”类,即一些具有explicit operator定义的特殊类,可自动转换为 DateTime/string 或从 DateTime/string 转换。但那是危险的领域。
回答by DropAndTrap
I did like this
我喜欢这个
var datetoEnter= DateTime.ParseExact(createdDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
回答by Pabitra Dash
The following works for me.
以下对我有用。
string strToday = DateTime.Today.ToString("MM/dd/yyyy");
回答by Fernando Mallorquín
You can change the format too by doing this
您也可以通过这样做来更改格式
string fecha = DateTime.Now.ToString(format:"dd-MM-yyyy");
// this change the "/"for the "-"
// 这改变"/"了"-"
回答by Mark Macneil Bikeio
Solution
解决方案
DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)

