C# 将字符串转换为时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18919530/
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-08-10 13:36:01  来源:igfitidea点击:

Convert string to Time

c#asp.net

提问by Apollo

I have a time that is 16:23:01. I tried using DateTime.ParseExact, but it's not working.

我有一个时间是 16:23:01。我尝试使用DateTime.ParseExact,但它不起作用。

Here is my code:

这是我的代码:

string Time = "16:23:01"; 
DateTime date = DateTime.ParseExact(Time, "hh:mm:ss tt", System.Globalization.CultureInfo.CurrentCulture);

lblClock.Text = date.ToString();

I want it to show in the label as 04:23:01 PM.

我希望它在标签中显示为 04:23:01 PM。

采纳答案by Jon Skeet

"16:23:01" doesn't match the pattern of "hh:mm:ss tt" - it doesn't have an am/pm designator, and 16 clearly isn't in a 12-hour clock. You're specifying that format in the parsingpart, so you need to match the format of the existing data. You want:

“16:23:01”与“hh:mm:ss tt”的模式不匹配 - 它没有 am/pm 指示符,而且 16 显然不在 12 小时制中。您在解析部分指定了该格式,因此您需要匹配现有数据的格式。你要:

DateTime dateTime = DateTime.ParseExact(time, "HH:mm:ss",
                                        CultureInfo.InvariantCulture);

(Note the invariant culture, notthe current culture - assuming your input genuinely always uses colons.)

(注意不变的文化,而不是当前的文化 - 假设您的输入确实总是使用冒号。)

If you want to formatit to hh:mm:ss tt, then you need to put that part in the ToStringcall:

如果要将其格式化hh:mm:ss tt,则需要将该部分放入ToString调用中:

lblClock.Text = date.ToString("hh:mm:ss tt", CultureInfo.CurrentCulture);

Or better yet (IMO) use "whatever the long time pattern is for the culture":

或者更好(IMO)使用“无论文化的长期模式是什么”:

lblClock.Text = date.ToString("T", CultureInfo.CurrentCulture);

Also note that hhis unusual; typically you don'twant to 0-left-pad the number for numbers less than 10.

还要注意这hh是不寻常的;通常,您不想为小于 10 的数字添加 0-left-pad。

(Also consider using my Noda TimeAPI, which has a LocalTimetype - a more appropriate match for just a "time of day".)

(还可以考虑使用我的Noda TimeAPI,它有一个LocalTime类型 - 更适合“一天中的某个时间”。)

回答by bhs

string Time = "16:23:01";
DateTime date = DateTime.Parse(Time, System.Globalization.CultureInfo.CurrentCulture);

string t = date.ToString("HH:mm:ss tt");

回答by bhs

This gives you the needed results:

这为您提供了所需的结果:

string time = "16:23:01";
var result = Convert.ToDateTime(time);
string test = result.ToString("hh:mm:ss tt", CultureInfo.CurrentCulture);
//This gives you "04:23:01 PM"  string

You could also use CultureInfo.CreateSpecificCulture("en-US")as not all cultures will display AM/PM.

您也可以使用,CultureInfo.CreateSpecificCulture("en-US")因为并非所有文化都会显示 AM/PM。

回答by Aleks

The accepted solution doesn't cover edge cases. I found the way to do this with 4KB script. Handle your input and convert a data.

接受的解决方案不包括边缘情况。我找到了使用 4KB 脚本执行此操作的方法。处理您的输入并转换数据。

Examples:

例子:

00:00:00 -> 00:00:00
12:01 -> 12:01:00
12 -> 12:00:00
25 -> 00:00:00
12:60:60 -> 12:00:00
1dg46 -> 14:06

You got the idea... Check it https://github.com/alekspetrov/time-input-js

你明白了……检查一下https://github.com/alekspetrov/time-input-js