## 5 replies

Last post Dec 09, 2014 03:05 AM by Lokesh B R

Participant

1347 Points

2439 Posts

### Convert integer to Hour C#

i want develop a routine where i will pass a integer value and the routine will return hour in 24 hrs format.

if i pass 0 then it will return 00

if i pass 5 then it will return 05

if i pass 12 then it will return 12

if i pass 14 then it will return 02

i tried this way but no luck

```            TimeSpan ts = TimeSpan.FromHours(12);
DateTime dt= Convert.ToDateTime(ts.ToString());

the above code is working but when i pass 0 then this line dt.ToString("hh"); returning 12 but i want it should return "00"

so guide me what to do. thanks

• ### Rion William...

All-Star

113100 Points

18367 Posts

MVP

### Re: Convert integer to Hour C#

Dec 08, 2014 09:33 AM|Rion Williams|LINK

Basically, you'll just need to use the modulo '%' operator which will always return the remainder after division occurs. In this case, you'll use it by '12' to ensure that values greater than 12 will still give you the values you are looking for :

```public string ConvertIntegerToHours(int i)
{
// If your integer is greater than 12, then use the modulo approach, otherwise output the value (padded)
return (i > 12) ? (i % 12).ToString("00") : i.ToString("00");
}```

You can see a working example here which iterates through 1-23 and displays the results :

``` 1 : 01
2 : 02
3 : 03
4 : 04
5 : 05
6 : 06
7 : 07
8 : 08
9 : 09
10 : 10
11 : 11
12 : 12
13 : 01
14 : 02
15 : 03
16 : 04
17 : 05
18 : 06
19 : 07
20 : 08
21 : 09
22 : 10
23 : 11```

Star

9011 Points

2395 Posts

### Re: Convert integer to Hour C#

Dec 08, 2014 09:46 AM|Lokesh B R|LINK

string input = "3:45";

var timeFromInput = DateTime.ParseExact(input, "H:m", null, System.Globalization.DateTimeStyles.None);

string timeIn12HourFormat = timeFromInput.ToString("hh", System.Globalization.CultureInfo.InvariantCulture);

Mark the post as answer, if it helps you.

Participant

1347 Points

2439 Posts

### Re: Convert integer to Hour C#

thanks Rion. i solved it this way (i == 0 ? "00" : Convert.ToDateTime(TimeSpan.FromHours(i).ToString()).ToString("hh")

Star

14297 Points

5882 Posts

### Re: Convert integer to Hour C#

@mou_inn   imho, you need to spend more time reading MSDN; your code example shows you do not understand your own code.

Let me explain.

(a) a TimeSpan is not a DateTIme!  you have no need to convert a TimeSpan to a DateTime:  example:

```TimeSpan ts = TimeSpan.FromHours(12);
DateTime dt= Convert.ToDateTime(ts.ToString());
Console.WriteLine (ts);      // TimeSpan  http://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx
Console.WriteLine (dt);      // DateTime

output:

```12:00:00
2014-12-08 12:00:00
12```

"TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. "

(b) TimeSpan can be greater than 24 hours:

```DateTime endOfWWI = new DateTime(1918, 11, 11, 11, 0, 0);
TimeSpan interval = DateTime.Now - endOfWWI;
Console.WriteLine (endOfWWI);
Console.WriteLine (interval);
Console.WriteLine (interval.TotalDays);
Console.WriteLine (interval.TotalHours);
Console.WriteLine (interval.TotalMinutes);
Console.WriteLine (interval.TotalDays/365);  // approximate years```

output:

```1918-11-11 11:00:00
35091.02:24:21.3326079
35091.1002469052
842186.405925724
50531184.3555435
96.1400006764526```

(c)  This does not mean NOON, NOON is a TIME; your code means a TimeSpan of 12 hours.

`TimeSpan ts = TimeSpan.FromHours(12);  // 12 hours`

FWIW

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it

Star

9011 Points

2395 Posts

### Re: Convert integer to Hour C#

Dec 09, 2014 03:05 AM|Lokesh B R|LINK

did you try this

string input = "3:45"; // 22:45

var timeFromInput = DateTime.ParseExact(input, "H:m", null, System.Globalization.DateTimeStyles.None);

string timeIn12HourFormat = timeFromInput.ToString("hh", System.Globalization.CultureInfo.InvariantCulture);

Mark the post as answer, if it helps you.