简体   繁体   English

将日期从mm / dd / yyyy转换为dd MMM yyyy

[英]Convert a Date from mm/dd/yyyy to dd MMM yyyy

How Do I convert a datetime from mm/dd/yyyy to dd MMM yyyy? 如何将日期时间从mm / dd / yyyy转换为dd MMM yyyy? DateTime.ParseExact or DateTime.TryParseExact does not require a parameter that tells it what format I want it in. So how does it know that I want it to return the passed in date to dd MMM yyyy format? DateTime.ParseExact或DateTime.TryParseExact不需要告诉其所需格式的参数。那么如何知道我希望它将传入的日期返回为dd MMM yyyy格式?

DateTime result;
var dateTime = DateTime.TryParseExact("03/21/2013", "mm/dd/yyyy",CultureInfo.InvariantCulture,DateTimeStyles.None, out result);

The DateTime object you get back has no formatting - it's just the raw date and time. 返回的DateTime对象没有格式-只是原始日期和时间。 To convert it to your new format after you've got it in a DateTime object, you call ToString on it, like: 要将其放入DateTime对象后,将其转换为新格式,请在其上调用ToString ,例如:

string formattedDate = result.ToString("dd MMM yyyy");

A DateTime object does not store the date in string format. DateTime对象不以字符串格式存储日期。 Rather you may output the value of DateTime using the format you like when obtaining a string representation: 相反,您可以在获取字符串表示形式时使用所需的格式输出DateTime的值:

// assuming you have date DateTime object variable named dateTime
dateTime.ToString("dd MMM yyyyy");

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

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