如何分离组合框的时间部分
C# 问题...我从数据库接收日期时间类型 1/1/1900 08:00:00 AM,我只想获取时间部分并用 HH MM(上午或下午)填充三个组合框。我必须对我得到的两组不同的数据做同样的事情......不知道该怎么做......
任何人都可以帮忙吗?
这就是我目前得到的...我是新手:0)
DateTime bh = Convert.ToDateTime(puf.GetResults["Begin_Hour"].ToString());
DateTime eh = Convert.ToDateTime(puf.GetResults["End_Hour"].ToString());
string bhs=bh.ToShortTimeString();
string ehs=eh.ToShortTimeString();
C# problem ...I am recieving from a database a datetime type 1/1/1900 08:00:00 AM and I want to take only the time portion and populate three comboBoxes with HH MM (AM or PM). I have to do the same thing for two diffent sets of data I'm getting...Have no idea how to do it..
Can anyone help?
This is what i got for the moment...I am a newbie :0)
DateTime bh = Convert.ToDateTime(puf.GetResults["Begin_Hour"].ToString());
DateTime eh = Convert.ToDateTime(puf.GetResults["End_Hour"].ToString());
string bhs=bh.ToShortTimeString();
string ehs=eh.ToShortTimeString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所拥有的应该能够很好地工作,以便获得时间。
您可以使用
ToString()
方法,该方法具有重载,您可以在其中提供自定义格式。请参阅此 MSDN 链接。因此,您可以使用 bhs.ToString("t"); 来打印小时、分钟和 AM/PM。
What you have ought to work fine to just get the time.
You could use the
ToString()
method which has an overload where you can give your custom formats. Refer to this MSDN link.So you can use
bhs.ToString("t");
to print the hour, minutes and AM/PM.使用它(dt 是您的 DateTime 对象)
结果将采用您想要的格式:
“4:05 PM”
您可以在此处阅读更多信息:日期时间的字符串格式
Use this (dt is your DateTime object)
The result will be in your desired format:
"4:05 PM"
You can read more here: String Format for DateTime
如果您使用对象/数据表,则将 DisplayMember/ValueMember 属性与 FormatString 属性结合起来设置可能是一个不错的选择。设置
FormattingEnabled = true
、FormatString = "t"
、DisplayMember = ValueMember = "[Begin_Hour|End_Hour]"
。请注意,[|] 语法是从正则表达式借用的,这意味着任一值都可以使用。然后只需将基础数据源分配给 ComboBox.DataSource 属性即可。通常最好处理 UI 元素的显示/格式并保持数据元素不变,这就是为什么许多控件提供这些不同的数据显示属性。
If you're using objects/datatables, it might be a good option to just set the DisplayMember/ValueMember properties in conjunction with the FormatString property. Set the
FormattingEnabled = true
,FormatString = "t"
,DisplayMember = ValueMember = "[Begin_Hour|End_Hour]"
.Note that [|] syntax is borrowing from Regex, meaning either value will work. Then just assign the underlying datasource to the ComboBox.DataSource property. It's usually a good idea to handle display/formatting on your UI elements and keep the data elements unchanged, which is why a lot of controls provide these various data display properties.