简体   繁体   中英

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? 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 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. To convert it to your new format after you've got it in a DateTime object, you call ToString on it, like:

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

A DateTime object does not store the date in string format. Rather you may output the value of DateTime using the format you like when obtaining a string representation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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