Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

C#
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
 
Share this answer
 
Comments
koool.kabeer 29-Jul-10 8:35am    
totally correct and exactly needed solution for the question ....
Display it as a string, append CurrentHour to "0" if its value is less than 10.
BTW, where are you displaying it?
 
Share this answer
 

C#
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));


 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900