Skip to main content
 首页 » 编程设计

c#之将日期时间转换为自定义日期格式

2024年02月13日8linjiqin

我有一个 DateTime 对象,其值为 2011-08-11T01:03:29+00:00,它是从数据库返回的。

如何将其转换为 mm/dd/yyyy 格式,最终结果类型应该仅为 DateTime 对象,而不是字符串?

请您参考如下方法:

DateTime 对象只是日期的数字表示,表示为从恒定开始时间(例如,0000 年 1 月 1 日)开始的“滴答”。它不是字符串表示。要获得字符串表示形式,您可以对对象执行 ToString()。

要将 DateTime 转换为自定义格式的字符串,请使用

myDateTime.ToString("MM/dd/yyyy"); 

参见 http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx有关 DateTime.ToString() 自定义格式的完整详细信息。

参见 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."