vb.net 将 12 小时时间格式转换为 24 小时整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17057160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:55:10  来源:igfitidea点击:

Convert 12-hour time format to 24 hour integer?

c#.netvb.netstringparsing

提问by CJ7

In my app I have a drop-down box of strings that shows possible hours in 12-hour time for the user to select. The possible values are:

在我的应用程序中,我有一个字符串下拉框,显示 12 小时内可能的小时数供用户选择。可能的值为:

9am
10am
11am
12pm
1pm
2pm
3pm
4pm
5pm

What code will convert one of these strings to a 24 hour integer? For example, 10amshould be converted to 10and 4pmshould be converted to 16.

什么代码会将这些字符串之一转换为 24 小时整数?例如,10am应转换为10并且4pm应转换为16

回答by RJ Lohan

You can use DateTime.Parse(...) to get a DateTime value, and then reference the .Hourproperty for a result;

您可以使用 DateTime.Parse(...) 获取 DateTime 值,然后引用该.Hour属性以获得结果;

int h = DateTime.Parse("10am").Hour;   // == 10
int h2 = DateTime.Parse("10pm").Hour;  // == 22

DateTime.Parse is pretty liberal in what it allows, but obviously makes some assumptions internally. For example, in the above code, DateTime.Parse("10am")returns 10am on the current date in the current timezone (I think...). So, be aware of the context in which you use the API.

DateTime.Parse 在它允许的方面非常自由,但显然在内部做了一些假设。例如,在上面的代码中,DateTime.Parse("10am")返回当前时区当前日期的上午 10 点(我认为......)。因此,请注意您使用 API 的上下文。

回答by John Saunders

If you have a dropdown, why not set the values to be the integer values you desire:

如果您有下拉菜单,为什么不将值设置为您想要的整数值:

<asp:DropDownList runat="server" ID="hours">
    <asp:ListItem Value="9">9am</asp:ListItem>
    <asp:ListItem Value="10">10am</asp:ListItem>
    <!-- etc. -->
    <asp:ListItem Value="17">5pm</asp:ListItem>
</asp:DropDownList>

回答by SimpleVar

Considering the times are continuous, you can simplify the logic:

考虑到时间是连续的,可以简化一下逻辑:

var firstHourStr = box.Items[0].ToString();
var firstHour = int.Parse(firstHourStr.Replace("am", "").Replace("pm", ""));

if (firstHourStr.Contains("pm"))
{
    firstHour += 12;
}

var selectedHour = firstHour + box.SelectedIndex;

If the hours are static, and you know the first hour, you could have a const and simplify the process by much with var selectedHour = FIRST_HOUR + box.SelectedIndex.

如果时间是静态的,并且您知道第一个小时,则可以使用 const 并使用var selectedHour = FIRST_HOUR + box.SelectedIndex.

Also, I assumed valid formats as shown in the question.

另外,我假设了问题中显示的有效格式。

Final note: You'll need to handle the 12pmcase which causes problems, due to the nature of the hour 12 being a single second after "am".

最后一点:您需要处理12pm导致问题的情况,因为 12 小时的性质是“am”之后的一秒。

回答by Mitch

You could use DateTime.Parse, but that would not play nicely with internationalization.

您可以使用DateTime.Parse,但这不会很好地与国际化配合使用。

int hour = DateTime.Parse(stringValue).Hour;

Instead, just use DateTimeobjects in the ComboBoxand format them using FormatString:

相反,只需使用 中的DateTime对象ComboBox并使用 FormatString 格式化它们:

// In Constructor:
cbHours.Items.Add(new DateTime(2000, 1, 1, 8, 0, 0));
cbHours.Items.Add(new DateTime(2000, 1, 1, 10, 0, 0));
cbHours.Items.Add(new DateTime(2000, 1, 1, 13, 0, 0));
cbHours.FormatString = "h tt";

// In event handler
if (cbHours.SelectedIndex >= 0)
{
    int hour = ((DateTime)cbHours.SelectedItem).Hour
    // do things with the hour
}