从日期时间在C#中删除的时间和日期时间固定格式

编程入门 行业动态 更新时间:2024-10-17 13:35:12
本文介绍了从日期时间在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#中删除的时间和日期时间固定格式

本文发布于:2023-11-07 19:38:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   日期   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!