Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi frds,
I want to display the time in hours and minute format. I enter the hour and minute in 110 means it automatically take 01:10 AM or 01:10 PM. I don't Know how can I proceed this. Anyone help me.

Thank You
Posted
Comments
U. G. Leander 12-Nov-15 1:33am    
Take a look at this page, the DateTime class could help you:

http://www.csharp-examples.net/string-format-datetime/
Afzaal Ahmad Zeeshan 12-Nov-15 5:02am    
Also, this is not a good practice to accept datetime in this format.
Maciej Los 14-Nov-15 17:04pm    
You mean you want to create input mask?
Walkthrough: Working with the MaskedTextBox Control

C#
string str = "110";
var hr = str[0] <= 9 ? "0" + str[0] : str[0] ;
var min = str[1] + str[2];
var time = hr + ":" + min;

Not tested, but give it a try. :)

-KR
 
Share this answer
 
Comments
U. G. Leander 12-Nov-15 1:39am    
Good and straight to the point...better than what I suggested in my comment :-)
Krunal Rohit 12-Nov-15 2:07am    
Thanks mate :-)

-KR
George Jonsson 12-Nov-15 2:32am    
What will happen if the user enters 1155?
Krunal Rohit 12-Nov-15 4:44am    
ouch! RegExp is in the action :laugh:

-KR
George Jonsson 12-Nov-15 5:25am    
They are not as dangerous as they seem. :-)
You can solve this problem with a regular expression. The advantage of this approach is that you get built in validation if the user enters numbers that is not a valid time.

Expression
^((?<hour>[0-9])|(?<hour>[0-1][0-9]))(?<minute>[0-5][0-9])$

* ^ is the start anchor. It means that the regex engine starts at the beginning of the string
* ((?<hour>[0-9])|(?<hour>[0-1][0-9])) means either a single digit from 0-9 or two digits where the first has to be 0 or 1.
?<hour> is a named group and makes it easy to extract parts of the regex later.
* (?<minute>[0-5][0-9]) means that two digit will be matched where the first has to be within the range 0-5 and the second 0-9 -> 00 - 59.
* $ is the end anchor and it means that no unmatched characters can come at the end of the string.

The C# code is not very complicated.

Add this variable as a member of the class
C#
private static Regex timeExpression = new Regex("^((?<hour>[0-9])|(?<hour>[0-1][0-9]))(?<minute>[0-5][0-9])$");

Then you can use this code
C#
DateTime time;
string inputData = "110";
Match m = timeExpression.Match(inputData);
if (m.Success)
{
    int hour = int.Parse(m.Groups["hour"].Value);
    int minute = int.Parse(m.Groups["minute"].Value);
    time = new DateTime(0, 0, 0, hour, minute, 0);
}
else
{
    // Some sort of error handling
}

If you then want to present the time for the user you can do that in different ways using the DateTime.ToString() method.
C#
time.ToString("hh:mm");   // Should fit your case


How you take care of AM and PM is a different question.

I forgot to add that it is good to have a regular expression tool so you can easily test how it works.
This is the one I use: RegEx Tester[^]
 
Share this answer
 
v4
C#
var time1 = "110";
int time;
int.TryParse(time1,out time);
if (time > 0)
{
    var min = time1.Substring(time1.Length - 2, 2);
    var hr = Convert.ToInt32(time1.Substring(0,time1.Length - 2));
    var ampm = "";
    if(hr < 24)
    {
        if (hr < 12)
            ampm = "am";
        else
        {
            hr = hr - 12 ;
            ampm = "pm";
        }
    }
    else
        throw null; // throw exception
    Console.WriteLine(hr.ToString()+":"+min+ampm);
}
else
   throw null; // throw exception
Console.ReadLine();


Try this, a work around, but still
 
Share this answer
 
v2
Comments
FrostedSyntax 15-Nov-15 3:26am    
This doesn't work if he enters 110 like he said in the question
Sabarinathan_G 16-Nov-15 1:56am    
Try this.

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