gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:01:21 25 4
gpt4 key购买 nike

我正在使用这些代码在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com