[英]Converting string format to datetime in mm/dd/yyyy
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 格式的字符串转换为日期时间变量,但它应该保留为 mm/dd/yyyy 格式。
string strDate = DateTime.Now.ToString("MM/dd/yyyy");
Please help.请帮忙。
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 DateTime
typed object.这将为您提供键入 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);
You need an uppercase M
for the month part.月份部分需要大写的
M
string strDate = DateTime.Now.ToString("MM/dd/yyyy");
Lowercase m
is for outputting (and parsing) a minute (such as h:mm
).小写
m
用于输出(和解析)一分钟(例如h:mm
)。
eg a full date time string might look like this:例如,完整的日期时间字符串可能如下所示:
string strDate = DateTime.Now.ToString("MM/dd/yyyy h:mm");
Notice the uppercase/lowercase mM
difference.注意大写/小写的
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 NO way to store a custom DateTime
format information to use as default
as in .NET most string formatting depends on the currently set culture, ie请注意,没有办法存储自定义
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, ie some special class with explicit operator
defined that automatically translates to and from DateTime/string.此外,另一种简单的方法是为您的 DateTime 使用不同的“容器”或“包装器”class,即一些特殊的 class 定义了
explicit operator
,可以自动转换为日期时间/字符串或从日期时间/字符串转换。 But that is dangerous territory.但那是危险的领域。
Solution解决方案
DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
I did like this我喜欢这个
var datetoEnter= DateTime.ParseExact(createdDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
You can change the format too by doing this您也可以通过这样做来更改格式
string fecha = DateTime.Now.ToString(format:"dd-MM-yyyy");
// this change the "/"
for the "-"
// 这将
"/"
更改为"-"
The following works for me.以下对我有用。
string strToday = DateTime.Today.ToString("MM/dd/yyyy");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.