Dallin B - 25 days ago 6x
C# Question

# Converting time input from 24hr clock , to seconds, and back to 24 hr format

I am new to C# and am sure my issue lies in the math, but I can't for the life of me figure it out.

What I need to do is take a time window on a 24hr clock ie: 1000-1100
convert it to seconds, which I can do.

(1000 / 100)*60 = 600

(1100 / 100)*60 = 660

Get 25% of the result(15), which I can also do. (660 / 4)

Add that 25% of additional seconds to the previous end time (1100)

The issue is that I am getting an output of 1100 instead of 1115
The Question is how do I make it output as 1115.

I don't know if I'm allowed to post my code here or not but its fairly brief. So if seeing it would help I can post it.

I do use constants to define hour and hundred so no magice numbers, these numbers are just illustrating the math.

``````    //define constants
const int HUNDRED = 100;
const int HOUR = 60;
const int PERCENT = 4;
private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
// recieve start time input
string oldStart = oldStartTime.Text;
int oldTimeIn = int.Parse(oldStart);

// recieve end time input
string oldEnd = oldEndTime.Text;
int oldTimeEnd = int.Parse(oldEnd);

// convert start time to minutes
int hoursStart = (oldTimeIn / HUNDRED) * HOUR;
int minutesStart = oldTimeIn % HUNDRED;
int totalStart = hoursStart + minutesStart;

// convert end time to minutes
int hoursEnd = (oldTimeEnd / HUNDRED) * HOUR;
int minutesEnd = oldTimeEnd % HUNDRED;
int totalEnd = hoursEnd + minutesEnd;

// calulate 25% of travel time ( end - start )
int extraTime = totalEnd - totalStart;
int totalMinutes = (extraTime / PERCENT);

// convert back to hours
int newEndHours = (totalEnd + totalMinutes) / HOUR * HUNDRED;

// out put new end time
newEndTime.Text = \$"{newEndHours:d4}";
``````

``````int newEndHours = (totalEnd + totalMinutes) / HOUR * HUNDRED;
``````int newEndHours = (totalEnd + totalMinutes) / HOUR * HUNDRED + (totalEnd + totalMinutes) % HOUR;