将12小时格式转换为24小时格式 [英] convert 12 hour format to 24 hour format

查看:209
本文介绍了将12小时格式转换为24小时格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个下拉列表,一个具有从7到7的小时,另一个具有5分钟的时间间隔的分钟.

所以我想要的是如果用户选择

来自ddl的1,因此在数据库中插入时间时,应该将其存储为13:00而不是1.同样,我需要从1到7的时间,并以13:00、14:00、15的时间存储到datanase中: 00..etc ..

我在数据库中有时间数据类型,用于存储时间值.

请帮忙.

谢谢

I have two drop down list one has hour from 7 to 7 and other has minutes of time interval 5minutes.

So What I want is if user selectes

1 from the ddl so while inserting time in the database it should get store as 13:00 instead of 1. like wise i need the timing from 1 to 7 and get stored in to datanase with 13:00, 14:00, 15:00..etc..

I have time datatype in the database for storing the time value .

Please help.

Thank you

推荐答案

好吧,您无需进行任何转换.您要使用数据,而不是数据的字符串表示形式.如果使用的是System.DateTime,则此类对象没有格式".格式仅在您在UI上显示时才应显示,并且不涉及转换".您只能将System.DataTime.ToString与适当的参数一起使用.您需要做的就是使用格式说明符和/或区域性.请参阅:
http://msdn.microsoft.com/en-us/library/k494fzbf.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ht77y576.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx [ ^ ].

阅读这些帮助页面时,请注意这些方法的文化敏感性.现在,格式说明符.它们分为标准和自定义类别:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].

这就是您所需要的.

—SA
Well, you don''t need to convert anything. You want to work with data, not string representation of data. If you are using System.DateTime, such objects do not have "formats". The formats should appear only when you present them on the UI, and no "conversion" is involved. You only use System.DataTime.ToString with appropriate parameters. All you need to do is format specifiers and/or culture you want to use. Please see:
http://msdn.microsoft.com/en-us/library/k494fzbf.aspx[^],
http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^],
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^],
http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx[^].

Reading these help pages, pay attention for the culture-sensitive nature of these methods. And now, the format specifiers. They fall into standard and custom categories:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

This is all you need.

—SA



假设将数小时的DDL命名为DDLH.
请按照以下步骤操作:
1.将在DDLH中选择的包存储在字符串变量中.
2.在整数变量中,使用Int32.Parse(string)将字符串值转换为整数,然后将其加12.您将有24小时的时间.格式.
3.将值插入数据库.

为了给您提供方便的编码,这里有个快速的建议.尝试将数据库中的字段大部分保留为int,string(或text),float,double和其他方便的数据类型.您可以使用带遮罩的文本框,也可以使用 datetime选择器选择日期.对用户和用户来说都将很容易.

如果我误解了您的问题,或者您需要进一步澄清,请告诉我.

谢谢.
Hi,
Say that the ddl for hours be named DDLH.
Follow the steps below:
1. Store the balue selected in DDLH in a string varioable.
2. In an integer variable convert the string value into intger using Int32.Parse(string) and add 12 to it. You will have the time in 24hrs. format.
3. Insert the value into database.

Here is quick suggestion you might follow for convenience in coding. Try to keep the fields in your database mostly int, string(or text), float, double, and so other convenient data types. You may use a masked textbox or you can use a datetime picker for choosing date. It will be easy both for and the user.

In case I misunderstood your question or you require more clarification do let me know.

Thank You.


这篇关于将12小时格式转换为24小时格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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