从日期时间在C#中删除的时间和日期时间固定格式 [英] removing time from datetime in c# and retaining datetime format

查看:98
本文介绍了从日期时间在C#中删除的时间和日期时间固定格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从日期时间删除时间和存储日期时间格式输出?我不想来显示时间。

How can I remove time from datetime and store the output in datetime format? I do not want to show the time.

说我有

string d = "2/27/2013 4:18:53 PM"

我如何保存输出的日期时间变量,只有日期,而不是​​时间。

How can I store the output in a DateTime variable with only the date and not time.

我可以使用ToShortDateString(),但是它返回一个字符串,而不是日期时间。

I can use ToShortDateString() but then it return a string and not datetime.

我的最终目标是按时间顺序,如果所有的项目都在日期时间格式,而不是字符串,只能做排序的日期列。

My ultimate goal is to sort the date column chronologically which can only be done if all the entries are in datetime format and not string.

推荐答案

日期的DateTime 结构的属性会给你一个日期,但它总是有一个时间组件表示午夜(00:00:00)。如果你开始用字符串,你也许可以像这样的工作:

The Date property of the DateTime struct will give you a date but it will always have a time component that represents midnight ("00:00:00"). If you're starting with a string, you might be able to work with something like this:

DateTime d = DateTime.Parse("2/27/2013 4:18:53 PM").Date; // 2/27/2013 12:00:00 AM



只要确保你在执行您比较的DateTime 对象(即省略的ToString的所有使用实例())。

另外,您也可以格式化在排序时间你的约会格式

Alternatively, you can format your date in the "sortable" time format:

string d = DateTime.Parse("2/27/2013 4:18:53 PM").ToString("s");

string d = yourDateTime.ToString("s");

有关上述情况 D 2013-02-27T16:18:53 。如果按字母顺序排序,字符串会按时间顺序排列。

For the above case d would be 2013-02-27T16:18:53. When sorted alphabetically, the strings will be in chronological order.

这篇关于从日期时间在C#中删除的时间和日期时间固定格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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