vb.net - 如何将 VB.NET 中的时间格式从 24 更改为 12?

标签 vb.net time format

我正在使用这些代码在 VB.NET 中显示时间 它以 24 小时格式显示,而且我需要 12 小时格式

System.DateTime.Now.Hour
System.DateTime.Now.Minute
System.DateTime.Now.Second

示例:

14:12:42

我需要它:

02:12:42

谢谢。

最佳答案

使用String.Format 。例如:

String.Format("{0:T}", System.DateTime.Now)           //02:12:42 PM
String.Format("{0:hh:mm:ss}", System.DateTime.Now)    //02:12:42
String.Format("{0:hh:mm:ss tt}", System.DateTime.Now) //02:12:42 PM

此外,this网站对于总结使用 String.Format 的各种方法非常有帮助。请记住,文化可以对非自定义格式产生影响。上面使用 T(长时间格式)的第一个示例在我位于美国的电脑上运行得很好。但如果你说:

String.Format(System.Globalization.CultureInfo.InvariantCulture, _
              "{0:T}", System.DateTime.Now)

最终结果为 14:12:42。后两个示例是自定义格式,不受文化影响。

关于vb.net - 如何将 VB.NET 中的时间格式从 24 更改为 12?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5803959/

相关文章:

javascript - (准确)从用户输入获取时间的首选方法(在 UiApp - GAS 中)

C++ 时间戳和 diffTime

具有灵活回退值的 java.time DateTimeFormatter 解析

javascript - 如何在javascript中创建行高的倍数?

.net - 将图像和文本一起复制到剪贴板

c# - 将 ASMX 重定向到另一台服务器

string - 使用 slice 值的 Golang 字符串格式

java - 将 float 格式化为特定长度

c# - 将 HTML 转换为 Word 文档

vb.net - VB.NET 中的共享方法无法处理这个问题,为什么?