字符串转换为具体的DateTime格式

编程入门 行业动态 更新时间:2024-10-27 01:21:05
本文介绍了字符串转换为具体的DateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在google搜索了一会儿,对我的生活似乎无法找到一个解决方案。我认为这将是容易的,但它的时间太长和我转向StackOverflow的。

I've been googling for a while now and for the life of me can't seem to find a solution. I thought this would be easy but it's taking too long and am turning to stackoverflow.

我需要其中包含一个日期和时间为DateTime变量字符串转换。我已经格式化的确切格式我想将其存储在但字符串时我将其转换为它不断增加,我不希望秒的日期时间。我希望它保存为01/01/2010 09:00 AM。下面是我一直使用至今code:

I need to convert a string which contains a date and time to a DateTime variable. I've formatted the string in the exact format I want to store it in but when i convert it to a DateTime it keeps adding the seconds which I don't want. I want it stored as 01/01/2010 09:00AM. Here's the code I've been using so far:

DateTime.ParseExact(startTime,"MM/dd/yyyy hh:mmtt", null);

..但它不断追加秒到它。请指教。

..but it keeps appending seconds to it. Please advise.

推荐答案

如果它存储为DateTime数据类型,它的保存正确的,但你的UI是的显示它错误。 datetime数据类型总是秒(毫秒等),不管你如何设置的值。问题是它如何被显示给用户。

If it's stored as a DateTime data type, it's stored correctly, but your UI is displaying it wrong. The DateTime data type always has the seconds (milliseconds, etc) regardless of how you set the value. The problem is in how it's being displayed back to the user.

您需要显示在显示时你在正确的字符串格式所需的日期为

You need to display the date you want in the right string format at display time as in

Label1.Text = startTime.ToString("MM/dd/yyyy hh:mmtt");

编辑 - 添加

要在GridView格式化,在这里看到:peterkellner/2006/05/24/how-to-set-a-date-format-in-gridview-using-aspnet-20using-htmlen$c$c-property/

To format it in a GridView, see here: peterkellner/2006/05/24/how-to-set-a-date-format-in-gridview-using-aspnet-20using-htmlencode-property/

更多推荐

字符串转换为具体的DateTime格式

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

发布评论

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

>www.elefans.com

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