C# .NET - 24 hour time format in Masked textBOx?

Asked By Eswaran Radhakrishnan on 22-Sep-08 09:55 AM
Hi all,

I have maskedtextbox to get the time in 24 hour format. If the user enters 14:14 means I need to convert as 2:14 PM. If the user enters 09:50 means I need to get the time as 09:50 AM.

Should I do manually, or is there anything predefined function in Maskedtextbox?

Thanks
R. Eswaran.

use datepicker - Partha Mandayam replied to Eswaran Radhakrishnan on 22-Sep-08 10:16 AM

TRy using a Datepicker control - it's easier and gives better UI

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22617627.html

solution - Perry replied to Eswaran Radhakrishnan on 24-Sep-08 12:50 PM

Hi,

There are so many ways to get the time in 24-hours format in C#. Some of them I have listed below.

1. System.DateTime MyDateTime = System.DateTime.Now;
  MessageBox.Show(System.Convert.ToString(MyDateTime ));

2. DateTime.Parse(yourtime).ToString("HH/mm/ss");

3.

using C = System.Console; 
...
static void Main() {
   DateTime dateTime = DateTime.Now;
   C.WriteLine ("d = {0:d}", dateTime );  // mm/dd/yyyy
   C.WriteLine ("D = {0:D}", dateTime );  // month dd, yyyy
   C.WriteLine ("f = {0:f}", dateTime );  // day, month dd, yyyy hh:mm 
   C.WriteLine ("F = {0:F}", dateTime );  // day, month dd, yyyy HH:mm:ss AM/PM 
   C.WriteLine ("g = {0:g}", dateTime );  // mm/dd/yyyy HH:mm
   C.WriteLine ("G = {0:G}", dateTime );  // mm/dd/yyyy hh:mm:ss
   C.WriteLine ("M = {0:M}", dateTime );  // month dd
   C.WriteLine ("R = {0:R}", dateTime );  // ddd Month yyyy hh:mm:ss GMT
   C.WriteLine ("s = {0:s}", dateTime );  // yyyy-mm-dd hh:mm:ss (Sortable)
   C.WriteLine ("t = {0:t}", dateTime );  // hh:mm AM/PM
   C.WriteLine ("T = {0:T}", dateTime );  // hh:mm:ss AM/PM
 
   // yyyy-mm-dd hh:mm:ss (Sortable)
   C.WriteLine ("u = {0:u}", dateTime );  
 
   // day, month dd, yyyy hh:mm:ss AM/PM
   C.WriteLine ("U = {0:U}", dateTime );
 
   // month, yyyy (March, 2006)
   C.WriteLine ("Y = {0:Y}", dateTime );  
   C.WriteLine ("Month = " + dateTime.Month); // month number (3)
 
   // day of week name (Friday)
   C.WriteLine ("Day Of Week = " + dateTime.DayOfWeek);     
 
   // 24 hour time (16:12:11)
   C.WriteLine ("Time Of Day = " + dateTime.TimeOfDay);     
 
   // (632769991310000000)
   C.WriteLine("DateTime.Ticks = " + dateTime.Ticks);   
   // Ticks are the number of 100 nanosecond intervals since 01/01/0001 12:00am
   // Ticks are useful in elapsed time measurement.
   }
4. Using String.Format
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone

-Paresh