如何仅在DateTime对象中删除C#中的日期部分 [英] How to remove time portion of date in C# in DateTime object only

查看:147
本文介绍了如何仅在DateTime对象中删除C#中的日期部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除日期时间的时间部分,或者可能以对象形式的日期格式不以字符串的形式。

I need to remove time portion of date time or probably have the date in following format in object form not in the form of string.

06/26/2014 00:00:00:000

06/26/2014 00:00:00:000

我不能使用任何字符串转换方法,因为我需要对象形式的日期。

I can not use any string conversion methods as I need the date in object form.

我尝试首先将日期转换为字符串,从中删除时间特定的日期,但是一旦将其转换为DateTime对象,它再次添​​加12:00:00 AM。

I tried first converting the date to string, remove the time specific date from it, but it adds 12:00:00 AM as soon as I convert it to DateTime object back again.

推荐答案

您不能在没有时间的情况下创建一个DateTime对象。它总是有一些时间。

You can't create a DateTime object without a time in it. It always has some time in it.

如果要输出没有时间部分,可以使用格式字符串这样做:

If you want to output it without a time portion, you can use a format string to do so:

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

标准日期和时间格式字符串

自定义日期和时间格式字符串

正如其他人所说,您可以访问 date.Date 以获取具有任何特定时间信息的值,但仍将有一个00:00 AM的时间。

As others have said, you can access date.Date to get a value with any specific time information omitted, but that will still have a time of 00:00 AM.

这篇关于如何仅在DateTime对象中删除C#中的日期部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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