如何在 VB.net 中将 24 小时制转换为 12 小时制为 hh:mm AM/PM

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

How to convert a 24 hour time to 12 hour in VB.net as hh:mm AM/PM

vb.nettimestring-formattingdatetime-conversion

提问by eastboundr

So let's say I have 1400, I want to convert it into 2:00PM

假设我有 1400,我想将其转换为 2:00PM

I tried the following:

我尝试了以下方法:

Dim convertedTime As String = DateTime.ParseExact(theTime,"HHmm", Nothing)

And it would give me this:

它会给我这个:

6/12/2012 02:00:00 PM

2012/6/12 下午 02:00:00

I do not want the date part, neither do I need the seconds. All I need is 2:00PM

我不想要日期部分,我也不需要秒。我只需要下午 2:00

How could I achieve this? Thanks!

我怎么能做到这一点?谢谢!

回答by Guffa

The ParseExactmethod returns a DateTimevalue, not a string. If you assign it to a string variable you will be converting it automatically, which uses the standard formatting.

ParseExact方法返回一个DateTime值,而不是一个字符串。如果您将它分配给一个字符串变量,您将自动转换它,它使用标准格式。

If you want it in a specific format, then format the DateTimevalue as a string:

如果您希望它采用特定格式,请将DateTime值格式化为字符串:

Dim d As DateTime = DateTime.ParseExact(theTime,"HHmm", Nothing);
Dim convertedTime As String = d.ToString("hh:mm tt")

回答by Tim Schmelter

Dim theTime = New Date(2012, 6, 12, 14, 0, 0)
Dim formatted = theTime.ToString("h:mm tt", Globalization.CultureInfo.InvariantCulture)

Custom Date and Time Format Strings

自定义日期和时间格式字符串

回答by user2507265

Label1.Text = Format(Now, "hh:mm"): Label1's text= 10:26 (or whatever the time is)

Label1.Text = Format(Now, "hh:mm"): Label1's text= 10:26 (或任何时间)

Label1.Text = Format(Now, "hh:mm tt"): Label's text = 10:26 PM

Label1.Text = Format(Now, "hh:mm tt"):标签的文字 = 晚上 10:26

Label1.Text = Format(Now, "dddd dd, MMMM, YYYY"): Label1's text = Thursday 21, August, 2014 (or whatever the date is)

Label1.Text = Format(Now, "dddd dd, MMMM, YYYY"): Label1 的文本 = 2014 年 8 月 21 日星期四(或任何日期)

回答by user2786420

Label1.Text = Now.ToShortTimeString.ToString()   (10:26 PM)

Label1.Text = Now.ToLongTimeString.ToString()    (10:26:30 PM) 

回答by edCoder

Try This One...

试试这个...

  Dim TimeNow As String
  TimeNow = TimeOfDay.ToString("h:mm:ss tt")

回答by Dennis Traub

There are two ways to achieve this.

有两种方法可以实现这一点。

Option 1(using standard date and time format strings):

选项 1(使用标准日期和时间格式字符串):

Dim theTime As DateTime = new DateTime(2008, 4, 10, 6, 30, 0)
Dim convertedTime As String = 
    theTime.ToString("t", CultureInfo.CreateSpecificCulture("en-us"))

Option 2(using custom date and time format strings):

选项 2(使用自定义日期和时间格式字符串):

Dim theTime As DateTime = new DateTime(2008, 4, 10, 6, 30, 0)
Dim convertedTime As String = theTime.ToString("hh:mm tt")

In both cases convertedTimewill be 6:30 AM

在这两种情况下convertedTime都会6:30 AM