C#中的日期时间格式 [英] Date time format in C#
本文介绍了C#中的日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我用c#编写以下代码,则可以找到"4".
int CurrentHoure = DateTime.Now.Hour;
[当前日期时间是04:15:15 AM]
我想以两位数显示为"04".
有谁可以帮我吗?
在此先感谢
Rasadul Alam
If I write the following code in c# then I can found "4"
int CurrentHoure = DateTime.Now.Hour;
[Current date time is 04:15:15 AM]
I want to display it as "04" in double digit.
Is there any one to help me??
Thanks in advance
Rasadul Alam
推荐答案
DateTime dt = DateTime.Now;
string displayedTime = dt.ToString("hh:mm:ss tt");
string displayedHour = dt.Hour.ToString("00");
string displayedHourVariant = dt.Hour.ToString().PadLeft(2, "0"); // the same
将其显示为一个字符串,如果CurrentHour的值小于10,则将其附加到"0".
顺便说一句,您在哪里显示它?
Display it as a string, append CurrentHour to "0" if its value is less than 10.
BTW, where are you displaying it?
string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f",
"dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
// Output date and time using each custom format string.
foreach (string customFmt in customFmts)
Console.WriteLine("'{0}': {1}", customFmt,
dateValue.ToString(customFmt));
这篇关于C#中的日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文