I have a DateTime
object with value as 2011-08-11T01:03:29+00:00
which is returned from a database.
How can I convert this to mm/dd/yyyy
format where as the end result type should be DateTime
object only, not string?
A DateTime
object is only the numerical representation of the date as 'ticks' from a constant start time (for example, January 1, 0000). It is not the string representation. To get a string representation, you do ToString()
on the object.
To convert your DateTime to a string for your custom format, use
myDateTime.ToString("MM/dd/yyyy");
See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx for complete details about DateTime.ToString()
custom formats.
See http://msdn.microsoft.com/en-us/library/system.datetime.aspx :
"Internally, all DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight, January 1, 0001. The actual DateTime value is independent of the way in which that value appears when displayed in a user interface element or when written to a file."
Pass it as a string and parse it
format = "mm/dd/yyyy";
try {
result = DateTime.ParseExact(yourDate.ToString(format), format, provider);
}
catch (FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
DateTime有一个叫ToLongDateString(
)和ToShortDateString()
函数(我相信那些是正确的函数名),而short函数就是你想做的。
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.