字符串转换为具体的DateTime格式 [英] Convert string to specific DateTime format

查看:103
本文介绍了字符串转换为具体的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格式化,在这里看到:
<一href=\"http://peterkellner.net/2006/05/24/how-to-set-a-date-format-in-gridview-using-aspnet-20using-htmlen$c$c-property/\" rel=\"nofollow\">http://peterkellner.net/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: http://peterkellner.net/2006/05/24/how-to-set-a-date-format-in-gridview-using-aspnet-20using-htmlencode-property/

这篇关于字符串转换为具体的DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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