删除时间:秒:毫秒​​的日期对象 [英] Remove hours:seconds:miliseconds in DateTime object

查看:191
本文介绍了删除时间:秒:毫秒​​的日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建DateTime对象而产生的格式的字符串 MM:DD:YYYY

I'm trying to create a string from the DateTime object which yields the format mm:dd:yyyy.

传统的的DateTime 的对象当属 MM:DD:YYYY小时:分钟:秒AM / PM

有没有一种方法可以快速删除小时:分钟:秒AM / PM 的日期时间的部分,这样,当我把它转换的ToString( )只会导致 MM:DD:?YYYY

Is there a way to quickly remove the hrs:min:sec AM/PM portion of the DateTime so that when I convert it ToString() it will only result in mm:dd:yyyy?

推荐答案

要回答你的问题,没有 - 你将不得不将其存储在不同的类型。最简单的选择是使用一个字符串

To answer your question, no - you would have to store it in a different type. The most simple choice is to use a string.

string date = dateTime.ToString("MM:dd:yyyy");

不过我也很强烈建议在你的程序作为字符串内部存储日期。这将使它很难做任何计算或比较它们。另外我建议你对强迫一个特定的培养你的约会再presentation,因为它意味着你的应用程序如预期在其他文化比你可能不会工作。

However I'd also strongly advise against storing dates internally in your program as strings. This will make it difficult to do any calculations or comparisons on them. Furthermore I'd advise you against forcing a specific culture for your date representation as it means your application probably won't work as expected in other cultures than yours.

一个稍微复杂的方法是创建覆盖的ToString自定义类。我也想避免这一点,因为它仍然是难以用你的类型与标准库函数。您将有所有的时间来回转换。

A slightly more sophisticated approach is to create a custom class which overrides ToString. I'd also avoid this though, because it will still be difficult to use your type with the standard library functions. You will have to convert back and forth all the time.

只是把它作为一个DateTime和做转换为字符串只在presentation层。您可以使用 DateTime.ToShortDateString 来打印用户友好的文化知道的字符串。

Just leave it as a DateTime and do the conversion to string only in the presentation layer. You can use DateTime.ToShortDateStringto print a user friendly culture aware string.

这篇关于删除时间:秒:毫秒​​的日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆